题意:Given two sorted integer arrays A and B, merge B into A as one sorted array.
解题思路:归并排序的归并这一步的实现,原理很多地方都有。使用一个tmp临时数组进行归并
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):
tmp = [0 for i in range(m + n)]
i = 0; j = 0; k = 0
while i < m and j < n:
if A[i] <= B[j]:
tmp[k] = A[i]; i += 1
else:
tmp[k] = B[j]; j += 1
k += 1
if i == m:
while k < m + n:
tmp[k] = B[j]; k += 1; j += 1
else:
while k < m + n:
tmp[k] = A[i]; i += 1; k += 1
for i in range(0, m + n):
A[i] = tmp[i]