-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSortedArray.cc
More file actions
31 lines (27 loc) · 837 Bytes
/
MergeSortedArray.cc
File metadata and controls
31 lines (27 loc) · 837 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
// https://oj.leetcode.com/problems/merge-sorted-array/
namespace MergeSortedArray {
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int posA = m - 1;
int posB = n - 1;
int dest = m + n - 1;
while (posA >= 0 && posB >= 0) {
if (A[posA] >= B[posB]) {
A[dest] = A[posA];
posA--;
} else {
A[dest] = B[posB];
posB--;
}
dest--;
}
while (posB >= 0) {
A[dest] = B[posB];
dest--;
posB--;
}
// if array A has remaning elements, just leave it there
}
};
}