跳到主要内容

Merge Two Sorted Arrays

描述​

Given two sorted integer arrays A and B, merge B into A as one sorted array.

Note: You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.

分析​

无

代码​

# Merge Two Sorted Arrays
# 时间复杂度O(m+n),空间复杂度O(1)
class Solution:
def merge(self, A: list, m: int, B: list, n: int) -> None:
ia, ib, icur = m - 1, n - 1, m + n - 1
while ia >= 0 and ib >= 0:
A[icur] = A[ia] if A[ia] >= B[ib] else B[ib]
icur -= 1
if A[ia] >= B[ib]:
ia -= 1
else:
ib -= 1
while ib >= 0:
A[icur] = B[ib]
icur -= 1
ib -= 1

相关题目​