回答編集履歴
1
追記
test
CHANGED
@@ -61,3 +61,83 @@
|
|
61
61
|
}
|
62
62
|
|
63
63
|
```
|
64
|
+
|
65
|
+
[追記] 題意に沿ってみた。std::rotateだけは使わせてね♪
|
66
|
+
|
67
|
+
```C++
|
68
|
+
|
69
|
+
#include <iostream>
|
70
|
+
|
71
|
+
#include <vector>
|
72
|
+
|
73
|
+
#include <algorithm>
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
class Solution {
|
78
|
+
|
79
|
+
public:
|
80
|
+
|
81
|
+
void merge(std::vector<int>& nums1, int m, std::vector<int>& nums2, int n) {
|
82
|
+
|
83
|
+
std::vector<int>::iterator first1 = nums1.begin();
|
84
|
+
|
85
|
+
std::vector<int>::iterator last1 = first1 + m;
|
86
|
+
|
87
|
+
std::vector<int>::iterator first2 = nums2.begin();
|
88
|
+
|
89
|
+
std::vector<int>::iterator last2 = first2 + n;
|
90
|
+
|
91
|
+
// 両方要素があるうちは小さい方を積む
|
92
|
+
|
93
|
+
while ( first1 != last1 && first2 != last2 ) {
|
94
|
+
|
95
|
+
if ( *first1 < *first2 ) {
|
96
|
+
|
97
|
+
// 1側を積むのは"次へ進む"だけ
|
98
|
+
|
99
|
+
++first1;
|
100
|
+
|
101
|
+
} else {
|
102
|
+
|
103
|
+
// 2側を積むのはちょっと面倒、1側の末尾に積んでくるっと回転
|
104
|
+
|
105
|
+
*last1 = *first2++;
|
106
|
+
|
107
|
+
std::rotate(++first1, last1, last1+1);
|
108
|
+
|
109
|
+
++last1;
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
// 残った2側を全部積む
|
116
|
+
|
117
|
+
while( first2 != last2 ) { *last1++ = *first2++; }
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
};
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
int main() {
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
std::vector<int> nums1 = { 1, 2, 3, 0, 0, 0 };
|
130
|
+
|
131
|
+
std::vector<int> nums2 = { 2, 5, 6 };
|
132
|
+
|
133
|
+
Solution s;
|
134
|
+
|
135
|
+
s.merge(nums1, 3, nums2, 3);
|
136
|
+
|
137
|
+
for (int item : nums1 ) { std::cout << item << ' '; }
|
138
|
+
|
139
|
+
std::cout << std::endl;
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
```
|