Easy to follow java solution


  • 0
    M

    public void merge(int[] nums1, int m, int[] nums2, int n) {
    int cur1=m-1;
    int cur2=n-1;
    int cur= (m+n)-1;
    while(cur1>=0 && cur2>=0){
    if(nums1[cur1]>=nums2[cur2]){
    nums1[cur]=nums1[cur1];
    cur1--;
    }
    else{
    nums1[cur]=nums2[cur2];
    cur2--;
    }
    cur--;
    }
    int remaining=0;
    if(cur2>=0){
    remaining=cur2;
    for(int i=0;i<=remaining;i++){
    nums1[i]=nums2[i];
    }
    }
    }


Log in to reply
 

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