forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
50 lines (35 loc) · 954 Bytes
/
main.cpp
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
46
47
48
49
50
/// Source : https://leetcode.com/problems/merge-sorted-array/
/// Author : liuyubobobo
/// Time : 2019-02-07
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
/// Sorting
/// Time Complexity: O(nlogn)
/// Space Complexity: O(1)
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
assert(nums1.size() == m + n && nums2.size() == n);
for(int i = 0; i < n ; i ++ )
nums1[m + i] = nums2[i];
sort(nums1.begin(), nums1.end());
}
};
void print_vec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
vector<int> nums1 = {1, 3, 5, 7};
int m = nums1.size();
vector<int> nums2 = {2, 4, 6};
int n = nums2.size();
for( int i = 0 ; i < nums2.size() ; i ++ )
nums1.push_back(0);
Solution().merge(nums1, m, nums2, n);
print_vec(nums1);
return 0;
}