# Python 47ms Solution

• Simple but still long.
Any suggestion to make it more Pythonic?

``````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):
totalLength = m+n
while m > 0 and n >0:
if A[m-1] >= B[n-1]:
A[totalLength - 1] = A[m - 1]
m -= 1
totalLength -= 1
elif B[n - 1] >= A[m -1]:
A[totalLength - 1] = B[n-1]
n -= 1
totalLength -= 1
if n > 0:
A[0:n] = B[0:n]``````

• ``````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):
B.reverse()
while B:
b = B.pop()
for i,a in enumerate(A):
if b < a:
A.insert(i, b)
break
else:
A.append(b)
``````

Could you tell me why this code can't pass ?

Input: [], [1]
Output: [0]
Expected: [1]

• ``````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):
def sort(list1, type):
t = 0
if type == 'asc':
for i in xrange(0, len(list1)):
for j in xrange(0, len(list1)):
if list1[i] < list1[j]:
t = list1[i]
list1[i] = list1[j]
list1[j] = t
else:
for i in xrange(0, len(list1)):
for j in xrange(0, len(list1)):
if list1[i] > list1[j]:
t = list1[i]
list1[i] = list1[j]
list1[j] = t
return list1
A.extend(B)
A = sort(A, 'asc'))
``````

can anybody tell me why can't this pass? Failed in Input: [], [1] Output: [0] Expected: [1] as well

• ``````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):
while m and n:
(A[m + n - 1], m, n) = (A[m - 1], m - 1, n) if A[m - 1] > B[n - 1] else (B[n - 1], m, n - 1)
A[:n] = B[:n]``````

• ``````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):
C = []
for i in A:
C.append(i)
for i in B:
A.append(i)
i = 0
j = 0
k = 0
while i < len(C) and j < len(B):
if C[i] < B[j]:
A[k] = C[i]
i += 1
k += 1
else:
A[k] = B[j]
j += 1
k += 1
while i < len(C):
A[k] = C[i]
k += 1
i += 1
while j < len(B):
A[k] = B[j]
k += 1
j += 1
``````

I test in my machine: [] [1] and result is [1], but the judge is [0], why?

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.