Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion leetcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ LeetCode
| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|1|[Two Sum](https://https://leetcode.com/problems/two-sum/) | [C](./src/1.c)|Easy
|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [C](./src/2.c)|Medium|
|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [C](./src/3.c)|Medium|
|4|[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [C](./src/4.c)|Hard|
|7|[Reverse of a number with 32-bit overflow check](https://leetcode.com/problems/reverse-integer/) | [C](./src/7.c)|Easy|
|8|[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [C](./src/8.c) |Medium|
|9|[Pallidrome number without converting to string and 32-bt overflow check](https://leetcode.com/problems/palindrome-number/) | [C](./src/9.c)|Easy|
|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [C](./src/20.c)|Easy|
|21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [C](./src/21.c)|Easy|
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [C](./src/24.c)|Medium|
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [C](./src/26.c)|Easy|
|27|[Remove Element](https://leetcode.com/problems/remove-element/) | [C](./src/27.c)|Easy|
|28|[Implement strStr()](https://leetcode.com/problems/implement-strstr/) | [C](./src/28.c)|Easy|
|35|[Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [C](./src/35.c)|Easy|
|53|[Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | [C](./src/53.c)|Easy|
|82|[Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) | [C](./src/82.c)|Medium|
Expand Down Expand Up @@ -69,4 +74,4 @@ LeetCode
|938|[Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/) | [C](./src/938.c)|Easy|
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [C](./src/965.c)|Easy|
|977|[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [C](./src/977.c)|Easy|
|1189|[Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c)|Easy|
|1189|[Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c)|Easy|
43 changes: 43 additions & 0 deletions leetcode/src/2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *head = NULL;
struct ListNode *walk = NULL;
struct ListNode *tmp = NULL;

int carry = 0;
int val1 = 0;
int val2 = 0;
int val = 0;

while(l1 != NULL || l2 != NULL || carry) {
val1 = 0;
val2 = 0;
val = 0;

if(l1) {
val1 = l1->val;
l1 = l1->next;
}

if(l2) {
val2 = l2->val;
l2 = l2->next;
}

val = carry + val1 + val2;
carry = val / 10;

tmp = malloc(sizeof(struct ListNode));
tmp->val = val % 10;
tmp->next = NULL;

if(!head) {
head = walk = tmp;
} else {
walk->next = tmp;
walk = walk->next;
}
}

return head;
}

56 changes: 56 additions & 0 deletions leetcode/src/21.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Iterative approach */
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *list = NULL;
struct ListNode *tmp = NULL;

if (!l1)
return l2;
if (!l2)
return l1;

if (l1 && l2) {
if (l1->val < l2->val) {
list = tmp = l1;
l1 = l1->next;
} else {
list = tmp = l2;
l2 = l2->next;
}

while(l1 && l2) {
if (l1->val < l2->val) {
tmp->next = l1;
l1 = l1->next;
} else {
tmp->next = l2;
l2 = l2->next;
}
tmp = tmp->next;
}

if (l1)
tmp->next = l1;
if (l2)
tmp->next = l2;

return list;
}

return NULL;
}


/* Recursive approach */
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
if(!l1)
return l2;t
return l1;
if(l1->val < l2->val) {
l1->next = mergeTwoLists(l1->next, l2);
return l1;
} else {
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}

37 changes: 37 additions & 0 deletions leetcode/src/28.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
int strStr(char* haystack, char* needle) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your way is brutal force. Do you come up with more efficient solution? It is a typical problem for pattern search. For example: Rabin Karp, KMP, or Horspool, ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have implemented this a year ago and I do not want to blindly implement an algorithm, so I'll need some time for that. It is up to you, you can merge this PR and I'll raise another one with the optimised solution to this problem or keep it open till I come up with another approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I raise a new pull request or add the kmp implementation to this pull request.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if you have the chance

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I could not see my commits in mater branch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create a new one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I'll create a new PR qwith all the previous and will add KMP to it.

int i = 0;
int j = 0;
int k = 0;
int hlen = 0;
int nlen = 0;

if(needle == NULL || *needle == 0)
return 0;

if(haystack == NULL || *haystack == 0)
return -1;

hlen = strlen(haystack);
nlen = strlen(needle);

if(hlen < nlen)
return -1;

for(i = 0; i <= hlen - nlen; i++) {
j = 0;
if(haystack[i] != needle[j++])
continue;

k = i + 1;
for(; j < nlen; j++) {
if(haystack[k] != needle[j]) {
break;
} else
k++;
}
if(j == nlen)
return i;
}
return -1;
}

29 changes: 29 additions & 0 deletions leetcode/src/4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
int i = 0;
int j = 0;
int k = 0;
int mid = 0;
int len = nums1Size + nums2Size;
int arr[len];

while(i < nums1Size && j < nums2Size) {
if(nums1[i] > nums2[j])
arr[k++] = nums2[j++];
else
arr[k++] = nums1[i++];
}

while(i < nums1Size)
arr[k++] = nums1[i++];

while(j < nums2Size)
arr[k++] = nums2[j++];

mid = len / 2;
if (len % 2 == 1)
return (int)arr[mid];
else
return (arr[mid - 1] + arr[mid]) / 2.0;

}

2 changes: 0 additions & 2 deletions leetcode/src/8.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


int myAtoi(char * str){
int i = 0, len = strlen(str),p =0,flag =1;
long sum = 0;
Expand Down