【leetcode】Merge Sorted Array
题目描述
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 m and n respectively.
解题思路:
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(void)
def merge(self, A, m, B, n):
posa = m - 1
posb = n - 1
for i in range(m+n-1,-1,-1):
if posa < 0:
A[i] = B[posb]
posb -= 1
elif posb < 0:
return
elif A[posa] > B[posb]:
A[i] = A[posa]
posa -= 1
else:
A[i] = B[posb]
posb -= 1