File tree Expand file tree Collapse file tree 1 file changed +9
-26
lines changed Expand file tree Collapse file tree 1 file changed +9
-26
lines changed Original file line number Diff line number Diff line change 11#include <stdio.h>
22#include <stdlib.h>
33
4- static int binary_search (int * nums , int len , int target )
4+ static void merge (int * nums1 , int m , int * nums2 , int n )
55{
6- int low = -1 ;
7- int high = len ;
8- while (low + 1 < high ) {
9- int mid = high - (high - low ) / 2 ;
10- if (target > nums [mid ]) {
11- low = mid ;
6+ int i = m - 1 , j = n - 1 , k = nums1Size - 1 ;
7+ while (i >= 0 && j >= 0 && k >= 0 ) {
8+ if (nums1 [i ] >= nums2 [j ]) {
9+ nums1 [k -- ] = nums1 [i -- ];
1210 } else {
13- high = mid ;
11+ nums1 [ k -- ] = nums2 [ j -- ] ;
1412 }
1513 }
16- if (high == len || nums [high ] != target ) {
17- return - high - 1 ;
18- } else {
19- return high ;
20- }
21- }
2214
23- static void merge (int * nums1 , int m , int * nums2 , int n )
24- {
25- int i , j , len1 = m ;
26- for (i = 0 ; i < n ; i ++ ) {
27- int index = binary_search (nums1 , len1 , nums2 [i ]);
28- if (index < 0 ) {
29- index = - index - 1 ;
30- }
31- for (j = len1 - 1 ; j >= index ; j -- ) {
32- nums1 [j + 1 ] = nums1 [j ];
15+ if (i == -1 ) {
16+ while (j >= 0 ) {
17+ nums1 [k -- ] = nums2 [j -- ];
3318 }
34- nums1 [index ] = nums2 [i ];
35- len1 ++ ;
3619 }
3720}
3821
You can’t perform that action at this time.
0 commit comments