LeetCode 88.Merge two ordered arrays Java

Keywords: Java Algorithm leetcode

Title Description

Give you two arrays of integers nums1 and nums2 in a non-decreasing order, with two integers m and n representing the number of elements in nums1 and nums2, respectively.

Please merge nums2 to nums1 so that the merged arrays are also in a non-decreasing order.

Note: Ultimately, the merged array should not be returned by a function, but stored in the array nums1. To cope with this situation, nums1 has an initial length of m + n, where the first m elements represent the elements that should be merged and the last n elements are 0, which should be ignored.

Example 1:
Input: nums1 = [1,2,3,0,0], M = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Explanation: Need to merge [1,2,3] and [2,5,6].
The combined result is [1,2,2,3,5,6], where the italicized bold label is the element in nums 1.

Example 2:
Input: nums1 = [1], m = 1, nums2 = [], n = 0
Output: [1]
Explanation: Need to merge [1] and [].
The combined result is [1].

Example 3:
Input: nums1 = [0], m = 0, nums2 = [1], n = 1
Output: [1]
Explanation: Arrays that need to be merged are [] and [1].
The combined result is [1].
Note that there are no elements in nums1 because m = 0. The only 0 in nums1 is to ensure that the merged results are successfully stored in nums1.

Tips:

  • nums1.length == m + n
  • nums2.length == n
  • 0 <= m, n <= 200
  • 1 <= m + n <= 200
  • -109 <= nums1[i], nums2[j] <= 109

Advanced: Can you design and implement an algorithm with O(m + n) time complexity to solve this problem?

Source: LeetCode
Links:https://leetcode-cn.com/problems/merge-sorted-array

Personal Ideas (Complete Advancement)

Same as Official Solution 2

Create an additional array space to merge nums1 and nums2 into the new array.

Since both arrays are non-decreasing, a single iteration can complete the merge.

Do a loop again to copy the merged new array to nums1

Code

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int nums3[] = new int[m+n];

        for(int i=0,j=0;i+j < m+n;){
            if(i>=m){		//The merge of nums1 is complete
                nums3[i+j]=nums2[j];
                j++;
            }else if(j>=n){		//The merge of nums2 is complete
                nums3[i+j]=nums1[i];
                i++;
            }
            else if(nums2[j]>nums1[i]){
                nums3[i+j]=nums1[i];
                i++;
            }
            else if(nums2[j]<=nums1[i]){
                nums3[i+j]=nums2[j];
                j++;
            }
        }

        for(int i=0;i<m+n;i++){
            nums1[i]=nums3[i];
        }
    }
}

Result:

Official Questions

Method 1: Sort after direct merge

The most intuitive way is to put nums 2 at the end of nums 1, then sort the entire array directly.

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        for (int i = 0; i != n; ++i) {
            nums1[m + i] = nums2[i];
        }
        Arrays.sort(nums1);
    }
}

Method 2: Double Pointer

Method 1 does not take advantage of the sorted property of the array nums 1 nums 2. To take advantage of this property, we can use the double-pointer method. This method considers two arrays as queues, taking smaller numbers from the headers of two arrays at a time and putting them into the results.

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int p1 = 0, p2 = 0;
        int[] sorted = new int[m + n];
        int cur;
        while (p1 < m || p2 < n) {
            if (p1 == m) {
                cur = nums2[p2++];
            } else if (p2 == n) {
                cur = nums1[p1++];
            } else if (nums1[p1] < nums2[p2]) {
                cur = nums1[p1++];
            } else {
                cur = nums2[p2++];
            }
            sorted[p1 + p2 - 1] = cur;
        }
        for (int i = 0; i != m + n; ++i) {
            nums1[i] = sorted[i];
        }
    }
}

Method 3: Reverse double pointer
(No formulas will be copied, just screenshots)

Code:

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int p1 = m - 1, p2 = n - 1;
        int tail = m + n - 1;
        int cur;
        while (p1 >= 0 || p2 >= 0) {
            if (p1 == -1) {
                cur = nums2[p2--];
            } else if (p2 == -1) {
                cur = nums1[p1--];
            } else if (nums1[p1] > nums2[p2]) {
                cur = nums1[p1--];
            } else {
                cur = nums2[p2--];
            }
            nums1[tail--] = cur;
        }
    }
}

Author: LeetCode-Solution
Links:https://leetcode-cn.com/problems/merge-sorted-array/solution/he-bing-liang-ge-you-xu-shu-zu-by-leetco-rrb0/
Source: LeetCode
Copyright is owned by the author. For commercial reprinting, please contact the author for authorization. For non-commercial reprinting, please indicate the source.

Posted by Hebbs on Thu, 23 Sep 2021 09:18:48 -0700