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 (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are mand n respectively.
Solution: 最直观的是使用一个辅助list,不过题意里明确指出A有足够的空间容纳下A、B两个数组里的元素,所以应该寻找不需要多余空间的做法: 采用在A数组尾部插入数字的方法。
Running Time: O(n)
class Solution:
# @param A a list of integers
# @param m an integer, length of A
# @param B a list of integers
# @param n an integer, length of B
# @return nothing
def merge(self, A, m, B, n):
i, j, k = m-1, n-1, m+n-1
while i>=0 and j>=0:
if A[i] >= B[j]:
A[k] = A[i]
i -= 1
else:
A[k] = B[j]
j -= 1
k -= 1
while j>=0:
A[k] = B[j]
j -= 1
k -= 1
return A