美文网首页
88. 合并两个有序数组

88. 合并两个有序数组

作者: 寂灭天骄小童鞋 | 来源:发表于2020-03-17 18:42 被阅读0次

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

    func merge(_ nums1: inout [Int], _ m: Int, _ nums2: [Int], _ n: Int) {
        if n <= 0 {return}
        var length1 = m - 1
        var length2 = n - 1
        var count = m + n - 1
        
        while length1 >= 0 && length2 >= 0 {
            if nums1[length1] > nums2[length2] {
                nums1[count] = nums1[length1]
                length1 = length1 - 1
            } else {
                nums1[count] = nums2[length2]
                length2 = length2 - 1
            }
            count = count - 1
        }
        for idx in stride(from: 0, to: length2 + 1, by: 1) {
            nums1[idx] = nums2[idx]
        }
    }
    

    相关文章

      网友评论

          本文标题:88. 合并两个有序数组

          本文链接:https://www.haomeiwen.com/subject/sbxxyhtx.html