3 line simple C++ solution


  • 3
    F
    class Solution {
    public:
        void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
            int i1 = m-1, i2 = n-1;
            while(i1>=0 && i2>=0) nums1[i1+i2+1] = nums1[i1]>nums2[i2]?nums1[i1--]:nums2[i2--];
            while(i2>=0) nums1[i2] = nums2[i2--];
        }
    };

  • 0
    V

    For, inputs: nums1 = {1, 4, 5, 6, x, x} and nums2 = { 7, 8}, this algorithm will not work.
    There needs to be an additional case wherein (i1 != 0) should also be handled after second while loop.


Log in to reply
 

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