Javascript solution


  • 0
    A

    The problem statement doesnt mention that array could be of empty length. In Javascript array were presented like

    nums1 = [1, 2, 3, 0, 0, 0];
    m = 3;
    nums2 =  [4, 5, 6];
    n = 3;
    

    which wasnt clear enough unless you submit your solution for first attempt.

    var merge = function(nums1, m, nums2, n) {
        var finalLength = m + n;
        var j = 0;
        /* Merge Array2 into Array1 */
        for(var i = m; i < m+n; i++) {
            nums1[i] = nums2[j];
            j++
        }
        var i = 0;
        /* Element in Array1 at ith position should have all small elements on left of it i.e. < 0 --> i > */
        while(i < m+n-1 ) {
            var j = i+1;
            
            while(j < m+n) {
              if(nums1[i] > nums1[j]) {
                /* Swap Values */
                var temp = nums1[i];
                nums1[i] = nums1[j];
                nums1[j] = temp;
              }    
               j++; 
            }
            i++;
        }
    };
    
    

Log in to reply
 

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