-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_sorted_array.cpp
More file actions
31 lines (30 loc) · 842 Bytes
/
Copy pathmerge_sorted_array.cpp
File metadata and controls
31 lines (30 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
/**
* @param A: sorted integer array A which has m elements,
* but size of A is m+n
* @param B: sorted integer array B which has n elements
* @return: void
*/
void mergeSortedArray(int A[], int m, int B[], int n) {
// write your code here
if (m < 0 || n < 0 || A == NULL || B == NULL) {
return;
}
int i = m - 1;
int j = n - 1;
for (int k = m + n - 1; k >= 0; k--) {
if (i >= 0 && j >=0 ) {
if (A[i] > B[j]) {
A[k] = A[i--];
} else {
A[k] = B[j--];
}
} else if (i >= 0) {
return;
} else if (j >= 0) {
A[k] = B[j--];
}
}
}
};