- 可以用另一个vector存储
提示:
What if you fill the longer array from the end instead of start ?
注意到原数组nums1[i]上的数最终位置必然大于等于i
题目链接:88. 合并两个有序数组
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int tip1=m-1,tip2=n-1;
int tip=m+n-1;
while (tip>=0){
int now;
if (tip1<0){
now=nums2[tip2];
tip2--;
}
else if (tip2<0){
now=nums1[tip1];
tip1--;
break;
}
else if (nums1[tip1]>nums2[tip2]){
now=nums1[tip1];
tip1--;
}
else{
now=nums2[tip2];
tip2--;
}
//}
nums1[tip]=now;
tip--;
}
}
};