Simple clean solution 4ms in C++


  • 8
    class Solution {
    public:
        void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) 
        {
            int r1 = m-1, r2 = n-1, r = m+n-1;
            while(r1>=0 && r2>=0)
                nums1[r--] = nums1[r1]>nums2[r2]? nums1[r1--] : nums2[r2--];
            while(r2 >= 0)
                nums1[r--] = nums2[r2--];
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.