From 42d4fb57415a05f20d3b1c4c4b786465a8276e63 Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Sat, 5 Oct 2019 19:17:38 +0530 Subject: [PATCH 1/7] Solution for "2. Add Two Numbers" --- leetcode/README.md | 3 ++- leetcode/src/2.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 leetcode/src/2.c diff --git a/leetcode/README.md b/leetcode/README.md index 1718af9ddd..36138a8cd4 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -7,6 +7,7 @@ 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| |20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [C](./src/20.c)|Easy| |24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [C](./src/24.c)|Medium| @@ -66,4 +67,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| \ No newline at end of file +|1189|[Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c)|Easy| diff --git a/leetcode/src/2.c b/leetcode/src/2.c new file mode 100644 index 0000000000..1336f339f8 --- /dev/null +++ b/leetcode/src/2.c @@ -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; +} + From 4d11bc02227b747d3098bbc35b4d15c52ae2d3bd Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Sat, 5 Oct 2019 19:19:12 +0530 Subject: [PATCH 2/7] Solution for "4. Median of Two Sorted Arrays" --- leetcode/README.md | 1 + leetcode/src/4.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 leetcode/src/4.c diff --git a/leetcode/README.md b/leetcode/README.md index 36138a8cd4..4063f3213f 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -9,6 +9,7 @@ LeetCode |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| |20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [C](./src/20.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| diff --git a/leetcode/src/4.c b/leetcode/src/4.c new file mode 100644 index 0000000000..1ac71c4b52 --- /dev/null +++ b/leetcode/src/4.c @@ -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; + +} + From 439ef79f26c9aa264dce9587096cff34005e4150 Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Sat, 5 Oct 2019 19:20:01 +0530 Subject: [PATCH 3/7] Solution for "7. Reverse Integer" --- leetcode/README.md | 1 + leetcode/src/7.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 leetcode/src/7.c diff --git a/leetcode/README.md b/leetcode/README.md index 4063f3213f..22fb09cd12 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -10,6 +10,7 @@ LeetCode |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 Integer](https://leetcode.com/problems/reverse-integer/) | [C](.src/7.c)|Easy| |20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [C](./src/20.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| diff --git a/leetcode/src/7.c b/leetcode/src/7.c new file mode 100644 index 0000000000..9984dcc95d --- /dev/null +++ b/leetcode/src/7.c @@ -0,0 +1,17 @@ +int reverse(int x) { + int dig = 0; + int num = 0; + + while(x != 0) { + dig = x % 10; + x /= 10; + + if(num > INT_MAX / 10 || num == INT_MAX / 10 && dig > 7) return 0; + if(num < INT_MIN / 10 || num == INT_MIN / 10 && dig < -8) return 0; + + num = (num * 10) + dig; + } + + return num; +} + From 33f2893592810adfc5331c4e490e1bd034666beb Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Sat, 5 Oct 2019 19:22:10 +0530 Subject: [PATCH 4/7] Solution for "8. String to Integer (atoi)" --- leetcode/README.md | 1 + leetcode/src/8.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 leetcode/src/8.c diff --git a/leetcode/README.md b/leetcode/README.md index 22fb09cd12..2254b248c0 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -11,6 +11,7 @@ LeetCode |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 Integer](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| |20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [C](./src/20.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| diff --git a/leetcode/src/8.c b/leetcode/src/8.c new file mode 100644 index 0000000000..4dd2f0f45b --- /dev/null +++ b/leetcode/src/8.c @@ -0,0 +1,32 @@ +int myAtoi(char* str) { + int val = 0; + int i = 0; + int len = 0; + int dig = 0; + int neg_flag = 0; + + len = strlen(str); + if(!len) + return val; + + /* Get rid of whitespaces */ + for(i = 0; i < len && str[i] == ' '; i++); + + if(str[i] == '-') { + neg_flag = 1; + i++; + } else if(str[i] == '+') { + i++; + } + + for(; str[i] >= '0' && str[i] <= '9'; i++) { + dig = str[i] - '0'; + if(val > INT_MAX / 10 || val == INT_MAX / 10 && dig > 7) + return neg_flag ? INT_MIN : INT_MAX; + + val = (val * 10) + dig; + } + + return neg_flag ? (-1 * val) : val; +} + From d2523614cef8a72a87eac3660459a620b7857732 Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Sat, 5 Oct 2019 19:23:57 +0530 Subject: [PATCH 5/7] Soulution for "21. Merge Two Sorted Lists" --- leetcode/README.md | 1 + leetcode/src/21.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 leetcode/src/21.c diff --git a/leetcode/README.md b/leetcode/README.md index 2254b248c0..7c54d02e14 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -13,6 +13,7 @@ LeetCode |7|[Reverse Integer](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| |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| diff --git a/leetcode/src/21.c b/leetcode/src/21.c new file mode 100644 index 0000000000..8c0a9308fa --- /dev/null +++ b/leetcode/src/21.c @@ -0,0 +1,40 @@ +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; +} + From 082f9c8195c542504544b06449af8fc8ffa42dee Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Sat, 5 Oct 2019 19:25:08 +0530 Subject: [PATCH 6/7] Solution for "28. Implement strStr()" --- leetcode/README.md | 1 + leetcode/src/28.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 leetcode/src/28.c diff --git a/leetcode/README.md b/leetcode/README.md index 7c54d02e14..375d866868 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -17,6 +17,7 @@ LeetCode |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| diff --git a/leetcode/src/28.c b/leetcode/src/28.c new file mode 100644 index 0000000000..1f019718cd --- /dev/null +++ b/leetcode/src/28.c @@ -0,0 +1,37 @@ +int strStr(char* haystack, char* needle) { + 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; +} + From 58a77bdc50e9e6fb4dac68ef3674df9932a7f74d Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Mon, 7 Oct 2019 08:07:52 +0530 Subject: [PATCH 7/7] Adding recusrsive approach to "21. Merge Two Sorted Lists" --- leetcode/src/21.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/leetcode/src/21.c b/leetcode/src/21.c index 8c0a9308fa..5d9d0e7caa 100644 --- a/leetcode/src/21.c +++ b/leetcode/src/21.c @@ -1,3 +1,4 @@ +/* Iterative approach */ struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { struct ListNode *list = NULL; struct ListNode *tmp = NULL; @@ -38,3 +39,18 @@ struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { 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; + } +} +