Ac solution code


  • 2

    The basic idea is to add bigger one of nums1 and nums2 to the end of nums1.

    public void merge(int[] nums1, int m, int[] nums2, int n) {
    	int total = m + n - 1;
    	m = m - 1;
    	n = n - 1;
    	while (total >= 0) {// Add bigger one of nums1 and nums2 to the end of nums1.
    		if (n < 0 || (m >= 0 && nums1[m] > nums2[n]))
    			nums1[total--] = nums1[m--];
    		else 
    			nums1[total--] = nums2[n--];
    	}
    }

Log in to reply
 

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