My C code with 0ms


  • 0
    H
    void merge(int* nums1, int m, int* nums2, int n) {
        int i = m - 1, j = n - 1;
        int len = m + n - 1;
        while (i >= 0 && j >= 0){
    	    if (nums1[i] >= nums2[j])
    		    nums1[len--] = nums1[i--];
    	    else
    		    nums1[len--] = nums2[j--];
       }
       while (i>=0)
    	  nums1[len--] = nums1[i--];
       while (j>=0)
    	  nums1[len--] = nums2[j--];
    

    }


  • 0
    N

    I think while(i>=0) is unnecessary, because if nums2 is sorted done before nums1, the elements in nums1 are already placed correctly.


  • 0
    H

    Thank you for your advice


Log in to reply
 

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