forked from vamosdalian/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxiecheng1.cpp
72 lines (67 loc) · 1.61 KB
/
xiecheng1.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
#include "include.h"
#include <bits/stdc++.h>
using namespace std;
template <typename T>
vector<T> getNums(char c = ' '){
vector<T> result;
string str;
T tmp = 0;
bool minus = false;
getline(cin, str);
if(str.size() == 0) return result;
for(int i = 0; i < str.size(); i++){
if(str[i] == c){
result.push_back(tmp);
tmp = 0;
minus = false;
continue;
}
if(str[i] == '-'){
minus = true;
continue;
}
tmp = minus? tmp*10 - str[i] + '0' : tmp*10 + str[i] - '0';
}
result.push_back(tmp);
return result;
}
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int p = m + n;
if(m > 0 && n > 0) {
m--;
n--;
while(n >= 0) {
if(nums1[m] <= nums2[n]) {
p--;
nums1[p] = nums2[n];
n--;
} else {
p--;
nums1[p] = nums1[m];
if(m > 0) {
m--;
}
// 补丁
else {
for(int i = 0; i <= n; i++) {
nums1[i] = nums2[i];
}
}
}
}
}
// 补丁
else {
for(int i = 0; i < n; i ++) {
nums1[i] = nums2[i];
}
}
}
int main(int argc, char const *argv[])
{
vector<int> Bvec = getNums<int>(' ');
vector<int> Cvec = getNums<int>(' ');
merge(Bvec, Bvec.size(), Cvec, Cvec.size());
cout << Bvec[Bvec.size() / 2] << endl;
return 0;
}