-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSortedArray.cc
76 lines (59 loc) · 1.29 KB
/
MergeSortedArray.cc
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
#include <vector>
#include <string>
#include <iostream>
using std::vector;
using std::string;
using std::cin;
using std::cout;
using std::endl;
#include <cstdlib>
using std::atoi;
struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x), next(nullptr){}
ListNode(std::initializer_list<int> vals):val(0), next(nullptr){
if(0 == vals.size()){
return;
}
auto p = vals.begin();
this->val = *p;
auto last = this;
for(++p; p != vals.end(); ++p){
last->next = new ListNode(*p);
last = last->next;
}
}
};
std::ostream& operator<<(std::ostream& os, ListNode *head){
for(; head; head = head->next){
os << head->val << " ";
}
return os;
}
#include <algorithm>
#include <cstring>
class Solution{
public:
void merge(int A[], int m, int B[], int n){
int pos2 = m + n - 1;
if (pos2 < 0){
return;
}
int i = m - 1, j = n - 1;
while (i >= 0 && j >= 0){
if (A[i] > B[j]){
A[pos2--] = A[i--];
}else{
A[pos2--] = B[j--];
}
}
while (j >= 0){
A[pos2--] = B[j--];
}
}
};
int main(){
Solution slu;
return 0;
}