Posted on

Description

Submission

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

        int end = m + n - 1;
        int i = m - 1, j = n - 1;
        for(; i >= 0 && j >= 0; --end) {
            if(nums1[i] > nums2[j]) {
                nums1[end] = nums1[i--];
            } else {
                nums1[end] = nums2[j--];
            }
        }

        if(j >= 0) {
            for(; j >= 0; --end, --j) {
                nums1[end] = nums2[j];
            }
        }
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *