您的位置:首页 > 其它

【leetcode】Merge Sorted Array

2015-04-18 18:15 423 查看

题目描述

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: