Beats 95% C#


  • 0
    K
        public void Merge(int[] nums1, int m, int[] nums2, int n)
        {
            if (nums1.Count() == 0 || nums2.Count() == 0)
                return;
            
            int k = 0;
            int l = 0;
            for (int i  = 0; l < m && k < n && i < m + n; ++i){
                if (nums1[i] > nums2[k]){
                    int j = k;
                    while(j < n && nums2[j] <= nums1[i]){
                        j++;
                    }
                    ShiftArrayToRight(nums1, j - k, i, m - 1 + k);
                    while (k != j){
                        nums1[i++] = nums2[k++];
                    }
                }
                else
                {
                    l++;
                }
            }
            
            while(k < n){
                nums1[m + k] = nums2[k];
                k++;
            }
        }
        
        private void ShiftArrayToRight(int[] arr, int n, int start, int end){
            for(int i = end; i >= start; --i){
                arr[i + n] = arr[i];
            }
        }
    }

Log in to reply
 

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