Leetcode 88) Merge Sorted Array

image

My Solution

  • Start from the back is the key point!
class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        m--;n--;
        for(int i =nums1.length-1;i>=0;i--){
            if(m>=0 && n>=0){
                if(nums1[m]> nums2[n]){
                    nums1[i]=nums1[m];
                    m--;
                }else{
                    nums1[i]=nums2[n];
                    n--;
                }
            }else if(n>=0){
                nums1[i]=nums2[n];
                n--;
            }

        }
    }
}

© 2018. All rights reserved.

Powered by Hydejack v8.5.2