-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path088_MergeSortedArray.cpp
More file actions
43 lines (31 loc) · 1.01 KB
/
088_MergeSortedArray.cpp
File metadata and controls
43 lines (31 loc) · 1.01 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
#include <vector>
using namespace std;
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int mPtr = 0, nPtr = 0;
vector<int> solutionVector;
//iterate m+n times to iterate through all numbers in the merged list
for (int i = 0; i < m+n; i++) {
//if reached the end of the second list,
//or nums1 is less than or equal to nums2
//add the element from nums1 and continue
if ((nPtr == n || nums1[mPtr] <= nums2[nPtr]) && mPtr < m) {
solutionVector.push_back(nums1[mPtr]);
mPtr++;
}
//otherwise add the element from nums2
else {
solutionVector.push_back(nums2[nPtr]);
nPtr++;
}
}
nums1 = solutionVector;
}
};
int main(int c, char** v) {
vector<int> nums1({1,2,3,0,0,0});
vector<int> nums2({2,5,6});
Solution s;
s.merge(nums1, 3, nums2, 3);
}