1ms Java Solution


  • 0
    H
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int currIndexOfNums1 = m-1;
        int currIndexOfNums2 = n-1;
        for (int i = nums1.length-1; i >= 0; i--){
            if (currIndexOfNums1>= 0 && currIndexOfNums2 >= 0){
                if (nums1[currIndexOfNums1] >= nums2[currIndexOfNums2]){
                    nums1[i] = nums1[currIndexOfNums1];
                    currIndexOfNums1--;
                }
                else{
                    nums1[i] = nums2[currIndexOfNums2];
                    currIndexOfNums2--;
                }
            }
            else if(currIndexOfNums1 >= 0 && currIndexOfNums2 < 0){
                break;
            }
            else if(currIndexOfNums1 < 0 && currIndexOfNums2 >= 0){
                nums1[i] = nums2[currIndexOfNums2];
                currIndexOfNums2--;
            }
        }
        
    }

Log in to reply
 

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