LeetCode - #88 Merge two sorted arrays

Continue to create, accelerate growth! This is the 4th day of my participation in the "Nuggets Daily New Plan · June Update Challenge", click to view the details of the event

foreword

Our community will successively organize Gu Yi ( Netflix Growth Hacker, author of "The Way of the iOS Interview", ACE Professional Fitness Coach. )'s Swift algorithm problem solutions into a text version for everyone to learn and read.

We have updated the LeetCode algorithm to 86 issues so far, and we will keep the update time and progress ( released at 9:00 am on Monday, Wednesday, and Friday ). There will be a big improvement.

If you don't accumulate a small step, you can't go a thousand miles; if you don't accumulate a small stream, you can't make a river. The Swift community accompanies you to move forward. If you have suggestions and comments, please leave a message at the end of the article, we will try our best to meet your needs.

Difficulty Level: Easy

1. Description

You are given two integer arrays sum in non- decreasing order , and two other integers sum , representing the number of elements in and respectively.nums1nums2mnnums1nums2

Please merge nums2into nums1, so that the merged arrays are also arranged in non- decreasing order .

Note: Ultimately, the merged array should not be returned by the function, but stored in the nums1array . To cope with this situation, nums1the initial length of is m + n, where the mfirst element represents the element that should be merged, and the nlast element is 0, which should be ignored. nums2The length is n.

2. Example

Example 1

输入:nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
输出:[1,2,2,3,5,6]
解释:需要合并 [1,2,3] 和 [2,5,6] 。
合并结果是 [1,2,2,3,5,6] ,其中斜体加粗标注的为 nums1 中的元素。
复制代码

Example 2

输入:nums1 = [1], m = 1, nums2 = [], n = 0
输出:[1]
解释:需要合并 [1] 和 [] 。
合并结果是 [1] 。
复制代码

Example 3

输入:nums1 = [0], m = 0, nums2 = [1], n = 1
输出:[1]
解释:需要合并的数组是 [] 和 [1] 。
合并结果是 [1] 。
注意,因为 m = 0 ,所以 nums1 中没有元素。nums1 中仅存的 0 仅仅是为了确保合并结果可以顺利存放到 nums1 中。
复制代码

Restrictions:

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

3. Answers

 class MergeSortedArray {
    func merge(_ nums1: inout [Int], _ m: Int, _ nums2: [Int], _ n: Int) {
        var i = m - 1, j = n - 1
        
        while i >= 0 || j >= 0 {
            if j < 0 || (i >= 0 && nums1[i] > nums2[j]) {
                nums1[i + j + 1] = nums1[i]
                i -= 1
            } else {
                nums1[i + j + 1] = nums2[j]
                j -= 1
            }
        }
    }
}
复制代码
  • Main idea: merge from end to end to avoid overwriting.
  • Time Complexity: O(n)
  • Space Complexity: O(1)

Repository for the algorithm solution: LeetCode-Swift

Click to go to LeetCode practice

about us

We are jointly maintained by Swift enthusiasts. We will share the technical content centered on Swift combat, SwiftUI, and Swift foundation, and also organize and collect excellent learning materials.

Guess you like

Origin juejin.im/post/7102734653479469064