-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0088. Merge Sorted Array.cpp
More file actions
45 lines (36 loc) · 941 Bytes
/
0088. Merge Sorted Array.cpp
File metadata and controls
45 lines (36 loc) · 941 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<vector>
#include<algorithm>
void merge(std::vector<int>& nums1, int m, std::vector<int>& nums2, int n) {
for (int i = 0; i < n; i++) {
nums1[m + i] = nums2[i];
}
std::sort(nums1.begin(), nums1.end());
}
void printVec(const std::vector<int>& vec) {
for (size_t i = 0; i < vec.size(); i++) {
std::cout << vec[i] << " ";
}
std::cout << std::endl;
}
int main() {
// Example 1:
std::vector<int> vec1(6, 0); // Note that the tests in the LeetCode system are with already allocated memory. For that I use this!:
vec1[0] = 1;
vec1[1] = 2;
vec1[2] = 3;
std::vector<int> vec2 = { 2, 5, 6 };
merge(vec1, 3, vec2, 3);
printVec(vec1);
// Example 2:
std::vector<int> vec3 = { 1 };
std::vector<int> vec4 = { };
merge(vec3, 1, vec4, 0);
printVec(vec3);
// Example 3:
std::vector<int> vec5(1);
std::vector<int> vec6 = { 1 };
merge(vec5, 0, vec6, 1);
printVec(vec5);
return 0;
}