https://leetcode.com/problems/merge-sorted-array/description/
Merge from high index.
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
if(nums2.size() == 0) return;
for(int i=m-1, j=n-1, k=m+n-1; k>=0; k--) {
if(i<0) {
nums1[k] = nums2[j--];
continue;
}
if(j<0) return;
if(nums1[i]<=nums2[j]) nums1[k] = nums2[j--];
else nums1[k] = nums1[i--];
}
}
No comments:
Post a Comment