forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Leo Ma <[email protected]>
- Loading branch information
1 parent
de8ec4b
commit 1b5e876
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test merge_array.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
static int binary_search(int *nums, int len, int target) | ||
{ | ||
int low = -1; | ||
int high = len; | ||
while (low + 1 < high) { | ||
int mid = high - (high - low) / 2; | ||
if (target > nums[mid]) { | ||
low = mid; | ||
} else { | ||
high = mid; | ||
} | ||
} | ||
if (high == len || nums[high] != target) { | ||
return -high - 1; | ||
} else { | ||
return high; | ||
} | ||
} | ||
|
||
static void merge(int* nums1, int m, int* nums2, int n) | ||
{ | ||
int i, j, len1 = m; | ||
for (i = 0; i < n; i++) { | ||
int index = binary_search(nums1, len1, nums2[i]); | ||
if (index < 0) { | ||
index = -index - 1; | ||
} | ||
for (j = len1 - 1; j >= index; j--) { | ||
nums1[j + 1] = nums1[j]; | ||
} | ||
nums1[index] = nums2[i]; | ||
len1++; | ||
} | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int i; | ||
int nums1[] = { 1, 3, 5, 7, 9, 0, 0, 0, 0, 0 }; | ||
int nums2[] = { 2, 4, 6, 8, 10 }; | ||
int size1 = 5;//sizeof(nums1) / sizeof(*nums1); | ||
int size2 = sizeof(nums2) / sizeof(*nums2); | ||
merge(nums1, size1, nums2, size2); | ||
for (i = 0; i < sizeof(nums1) / sizeof(*nums1); i++) { | ||
printf("%d ", nums1[i]); | ||
} | ||
printf("\n"); | ||
return 0; | ||
} |