def merge(self, nums1, m, nums2, n):
while m > 0 and n > 0:
if nums1[m1] >= nums2[n1]:
nums1[m+n1] = nums1[m1]
m = 1
else:
nums1[m+n1] = nums2[n1]
n = 1
if n > 0:
nums1[:n] = nums2[:n]
Beautiful Python Solution


@tju_xu97 This is great. The use of OR operator helps escape the out of index range error which busted my mind for hours. Thanks a lot.

@tju_xu97 said in Beautiful Python Solution:
def merge(self, nums1, m, nums2, n):
while n > 0:
if m <= 0 or nums2[n1] >= nums1[m1]:
nums1[m+n1] = nums2[n1]
n = 1
else:
nums1[m+n1] = nums1[m1]
m = 1Why this is not working for the following ??
Input:
[0]
0
[1]
1
Output:
[0]
Expected:
[1]


@cffls
If I change the last line into nums1 = nums[:n]
the answer is wrong.
input:[0], 0, [1], 1
output: [0]
expected: [1]
But run the code in my pc that the output is [1].
don't konw the reason.


@qzhao123 This is wrong, since nums1 size could be larger than m+n, so if you sort nums1 you are sorting all the elements which is not the correct

@chch1992 input [0],0, I think the input is wrong, as '0' in [ ],it means it has 1 element,not 0.