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]
}
}
网友评论