-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Leetcode solutions #349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Leetcode solutions #349
Changes from all commits
42d4fb5
4d11bc0
439ef79
33f2893
d252361
082f9c8
90bb2aa
58a77bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } | ||
|
|
| 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; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| int strStr(char* haystack, char* needle) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should I raise a new pull request or add the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, if you have the chance
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, I could not see my commits in mater branch.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can create a new one
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
|
||
| 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; | ||
|
|
||
| } | ||
|
|
| 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; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.