题目:
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.
分析:
今晚不是很想出去。。看男神怎么说。。。
注意维护m的值。就酱。
男神说出去。。。
代码:
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
vector<int>::iterator it1=nums1.begin(),it2=nums2.begin();
int i=0,j=0;
int total=n+m;
while(j<n){
if(i<m&&*it1>=*it2){
it1=nums1.insert(it1,*it2);
it1++;
it2++;
i++;
j++;
m++;
}
else if(i==m){
it1=nums1.insert(it1,*it2);
it1++;
it2++;
j++;
}
else {
it1++;
i++;
}
}
it1=nums1.begin()+total;
while(it1!=nums1.end()){
it1=nums1.erase(it1);
}
}
};