forked from timoncui/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_Sorted_Array.cpp
More file actions
29 lines (24 loc) · 835 Bytes
/
Merge_Sorted_Array.cpp
File metadata and controls
29 lines (24 loc) · 835 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
/*
Author: Timon Cui, [email protected]
Title: Merge Sorted Array
Description:
Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note:
You may assume that A has enough space to hold additional elements from B.
The number of elements initialized in A and B are m and n respectively.
Difficulty rating: Easy
Notes:
The key is to observe that there are m + n elements in the merged list,
and if we merge from the back, the unmerged elements in A will never get
overwritten. This is O(m + n) and require O(1) extra space.
*/
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int a = m - 1, b = n - 1, w = m + n - 1;
while (a >= 0 || b >= 0) {
if (a >= 0 && (b < 0 || A[a] > B[b])) A[w--] = A[a--];
else A[w--] = B[b--];
}
}
};