Java solution with one loop and easy to understand


  • 0
    I
    public static void merge(int[] nums1, int m, int[] nums2, int n) {
    
    		int nums2Index = n - 1;
    		int nums1Index = m - 1;
    		int totalIndex = m + n - 1;
    		while (nums2Index >= 0) {
    			if (nums1Index < 0 || nums1[nums1Index] <= nums2[nums2Index]) {
    				nums1[totalIndex--] = nums2[nums2Index];
    				nums2Index--;
    			} else {
    				nums1[totalIndex--] = nums1[nums1Index];
    				nums1Index--;
    			}
    		}
    	}

Log in to reply
 

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