forked from cpselvis/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution088.cpp
More file actions
56 lines (49 loc) · 918 Bytes
/
solution088.cpp
File metadata and controls
56 lines (49 loc) · 918 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
46
47
48
49
50
51
52
53
54
55
/**
* Merge Sorted Array
* From right most to left.
*
* cpselvis ([email protected])
* August 30th, 2016
*/
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i = m - 1, j = n - 1, p = m + n - 1;
while (p >= 0)
{
if (i < 0)
{
nums1[p] = nums2[j --];
}
else if (j < 0)
{
nums1[p] = nums1[i --];
}
else if (nums1[i] >= nums2[j])
{
nums1[p] = nums1[i --];
}
else
{
nums1[p] = nums2[j --];
}
p --;
}
}
};
int main(int argc, char **argv)
{
Solution s;
int a[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
int b[] = {2, 4, 6, 8, 10};
vector<int> vec1(a + 0, a + 10);
vector<int> vec2(b + 0, b + 5);
s.merge(vec1, 5, vec2, 5);
for (auto i : vec1)
{
cout << i << endl;
}
}