-
Notifications
You must be signed in to change notification settings - Fork 0
/
88-MergeSortedArray.cpp
89 lines (77 loc) · 1.9 KB
/
88-MergeSortedArray.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
*/
#include <stdio.h>
#include <vector>
using namespace std;
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
if (m == 0 || nums1.empty()) {
nums1 = nums2;
return;
}
if (n == 0 || nums2.empty()) {
return;
}
size_t size1 = nums1.size() - m;
for (size_t i = 1; i <= size1; i++) {
nums1.pop_back();
}
for (size_t i = 1; i <= nums2.size() - n; i++) {
nums1.pop_back();
}
for (size_t i = 0; i < nums2.size(); i++) {
int key = nums2[i];
if (key < nums1[0]) {
nums1.push_back(key);
for (size_t j = nums1.size() - 1; j > 0; j--) {
nums1[j] = nums1[j - 1];
}
nums1[0] = key;
continue;
}
if (key > nums1[nums1.size() - 1]) {
nums1.push_back(key);
continue;
}
for (size_t j = 0; j < nums1.size() - 1; j++) {
if ((nums1[j] <= key) && (key <= nums1[j + 1])) {
nums1.push_back(key);
// Note that nums1.size() has increased by one
for (size_t k = nums1.size() - 1; k > j; k--) {
nums1[k] = nums1[k - 1];
}
nums1[j + 1] = key;
break;
}
}
}
}
};
int main() {
int array1[6] = {1, 2, 3, 0, 0, 0};
vector <int> source1;
vector <int> source;
for (int i = 0; i < 6; i++) {
printf("%d ", array1[i]);
source1.push_back(array1[i]);
}
printf("\n");
int array2[3] = {2, 5, 6};
vector <int> source2;
for (int i = 0; i < 3; i++) {
printf("%d ", array2[i]);
source2.push_back(array2[i]);
}
printf("\n");
Solution s;
s.merge(source1, 3, source2, 3);
for (size_t i = 0; i < source1.size(); i++) {
printf("%d ",source1[i]);
}
printf("\n");
return 0;
}