cpp-interview/Problems/LeetcodeProblems/88-merge-sorted-array.h
2018-02-11 22:17:45 +08:00

20 lines
571 B
C++

class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i1 = m - 1, i2 = n - 1;
for(int i = m + n - 1; i >= 0; i--) {
if(i1 >= 0 && i2 < 0)
break;
if(i1 < 0 && i2 >= 0) {
nums1[i] = nums2[i2--];
}
if(i1 >= 0 && i2 >= 0) {
if(nums1[i1] > nums2[i2]) {
nums1[i] = nums1[i1--];
} else {
nums1[i] = nums2[i2--];
}
}
}
}
};