-
Notifications
You must be signed in to change notification settings - Fork 1
/
MergeSortedArray.py
27 lines (25 loc) · 1.29 KB
/
MergeSortedArray.py
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
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):
last, i, j = m + n - 1, m - 1, n - 1
while i >= 0 and j >= 0:
if A[i] > B[j]:
A[last] = A[i]
last, i = last - 1, i - 1
else:
A[last] = B[j]
last, j = last - 1, j - 1
while j >= 0:
A[last] = B[j]
last, j = last - 1, j - 1
if __name__ == "__main__":
A = [1, 3, 5, 0, 0, 0, 0]
B = [2, 4, 6, 7]
Solution().merge(A, 3, B, 4)
print A