0ms Java easy understand solution


  • 0
    S
    public class Solution {
        public void merge(int[] nums1, int m, int[] nums2, int n) {
            int i = 0;
    		int j = 0;
    		int[] temp = Arrays.copyOfRange(nums1, 0, m);
    		while (i<m || j<n) {
    		    if (i==m && j<n){
    				nums1[i+j] = nums2[j];
    				j++;
    				continue;
    			}
    			if (j==n && i<m){
    				nums1[i+j] = temp[i];
    				i++;
    				continue;
    			}
    			if (temp[i]<nums2[j]){
    				nums1[i+j] = temp[i];
    				i++;
    			}else {
    				nums1[i+j] = nums2[j];
    				j++;
    			}
    		}
        }
    }
    

Log in to reply
 

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