-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeTwoSortedArrays.py
42 lines (42 loc) · 1.18 KB
/
mergeTwoSortedArrays.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution:
"""
@param A: sorted integer array A
@param B: sorted integer array B
@return: A new sorted integer array
"""
def mergeSortedArray(self, A, B):
# write your code here
C = []
left, right = 0, 0
while left < len(A) and right < len(B):
if A[left] < B[right]:
C.append(A[left])
left += 1
else:
C.append(B[right])
right += 1
if left < len(A):
for _ in xrange(left, len(A)):
C.append(A[_])
if right < len(B):
for _ in xrange(right, len(B)):
C.append(B[_])
return C
def merge(self, A, B):
# write your code here
C = []
left, right = 0, 0
while left < len(A) and right < len(B):
if A[left] < B[right]:
C.append(A[left])
left += 1
else:
C.append(B[right])
right += 1
while left < len(A):
C.append(A[left])
left += 1
while right < len(B):
C.append(B[right])
right += 1
return C