Merge Sorted Array - LeetCode
题目:
Given two sorted integer arrays A and B, merge B into A as one sorted array.
分析:
这里要注意的是这里处理list不能用append,我猜想是在一开始时A的真是长度就是m+n,所以你用append时,最后部分都将会0。
代码:
def merge(self, A, m, B, n):
if m == 0:
A[0:n] = B[0:n]
elif n == 0:
return
else:
i,j = 0,0
if B[0] >= A[m-1]:
A[m:] = B[0:n]
return
while j < len(A) and i < n:
if B[i] <= A[j]:
A.insert(j,B[i])
i+=1
j +=1
A[m+i:] = B[i:]