Super Entry-Level and clear code for those are new to CS!


  • 0
    Y
    public class Solution {
        public void merge(int[] nums1, int m, int[] nums2, int n) {
            int i = m-1;
            int j = n-1;
            for(int k = m+n-1; k > -1; k--){
                if(i<0){
                    nums1[k] = nums2[j];
                    j--;
                }
                else if( j< 0){
                    nums1[k] = nums1[i];
                    i--;
                }
                else if( nums1[i] < nums2[j]){
                    nums1[k] = nums2[j];
                    j--;
                }
                else{
                    nums1[k] = nums1[i];
                    i--;
                }
            }
        }
    }
    

Log in to reply
 

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