LintCode Q64 Merge Sorted Array in Python

  • Jinhai ZHOU
  • 3 Minutes
  • 2016年11月30日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution:
"""
@param A: sorted integer array A which has m elements,
but size of A is m+n
@param B: sorted integer array B which has n elements
@return: void
"""
def mergeSortedArray(self, A, m, B, n):
# write your code here
index_A = m - 1
index_B = n - 1
current = m + n - 1
while index_B >= 0 and index_A >= 0:
if A[index_A] >= B[index_B]:
A[current] = A[index_A]
index_A -= 1
else:
A[current] = B[index_B]
index_B -= 1
current -= 1
while index_B >= 0:
A[current] = B[index_B]
index_B -= 1
current -= 1
while index_A >= 0:
A[current] = A[index_A]
index_A -= 1
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。