Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1341 from julienChemillier/patch-28
Browse files Browse the repository at this point in the history
Add 88 in c language
  • Loading branch information
Ahmad-A0 authored Nov 2, 2022
2 parents 717cfa9 + e49407a commit e565c64
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions c/88-Merge-Sorted-Array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Merge nums1 and nums2 into a single array sorted in non-decreasing order.
Space: O(1)
Time: O(n+m)
*/

void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){
int i=m-1;
int j=n-1;
while (i>=0 || j>=0) {
if (j<0){ // Only numbers from nums1 remain
return;
} else if (i<0) { // Only numbers from nums2 remain
nums1[j] = nums2[j];
j--;
} else {
if (nums1[i]>nums2[j]) {
nums1[i+j+1] = nums1[i];
i--;
} else {
nums1[i+j+1] = nums2[j];
j--;
}
}
}
}

0 comments on commit e565c64

Please sign in to comment.