-
Notifications
You must be signed in to change notification settings - Fork 0
/
200128-1.cpp
51 lines (48 loc) · 883 Bytes
/
200128-1.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
// https://leetcode-cn.com/problems/merge-sorted-array/
#include <cstdio>
#include <vector>
using namespace std;
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
if (n == 0) return;
int* a = &nums1[0];
int* b = &nums2[0];
int* target = a + (m + n - 1);
int* a_end = a - 1;
int* b_end = b - 1;
a += m - 1;
b += n - 1;
for (;;) {
if (a != a_end && b != b_end) {
if (*a >= *b) {
*target-- = *a--;
} else {
*target-- = *b--;
}
} else if (a != a_end) {
*target-- = *a--;
} else if (b != b_end) {
*target-- = *b--;
} else {
break;
}
}
}
};
void print(const vector<int>& a)
{
for (auto e : a) { printf("%d ", e); }
printf("\n");
}
int main()
{
Solution s;
{
vector<int> a = {1,2,3,0,0,0};
vector<int> b = {2,5,6};
s.merge(a, 3, b, 3);
print(a);
}
return 0;
}