Why my python code has an error?


  • 0
    W
    class Solution(object):
        def merge(self, nums1, m, nums2, n):
            if m == 0:
                nums1 = nums2
            nums3 = []
            i = 0
            j = 0
            while m != 0 and n != 0:
                if nums1[i] <= nums2[j]:
                    nums3.append(nums1[i])
                    i += 1
                else:
                    nums3.append(nums2[j])
                    j += 1
                if i == len(nums1):
                    for k in range(j+1, len(nums2)):
                        nums3.append(nums2[k])
                    nums1 = nums3
                    break
                elif j == len(nums2):
                    for k in range(i+1, len(nums1)):
                        nums3.append(nums1[k])
                    nums1 = nums3
                    break
    

    The above code has an error:

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

    I ran it in pycharm and it came out with right answer.


Log in to reply
 

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