"Merge Sorted Array" python test case

  • 0


    The test case ([0], 0, [1], 1) failed to pass, but worked fine on my on environment.

    class Solution:
        # @param {integer[]} nums1
        # @param {integer} m
        # @param {integer[]} nums2
        # @param {integer} n
        # @return {void} Do not return anything, modify nums1 in-place instead.
        def merge(self, nums1, m, nums2, n):
            if not m:
                nums1 = nums2[:n]
            elif not n:
                nums1 = nums1[:m]
                i = j = 0
                while j < n:
                    while nums1[i] < nums2[j]:
                        i += 1
                        if i == len(nums1):
                    if i == len(nums1):
                        nums1[i+1:] = nums2[-n+j:]
                    nums1.insert(i, nums2[j])
                    j += 1

    Please help.

  • 0

    worked fine on my on environment.

    No it didn't.

    For that test case, you're not modifying the original nums1 at all. You're creating a new list that the caller can't even see.

    You could do it like this:

    nums1[:n] = nums2[:n]

  • 0

    Thanks, I passed all test cases with 2 lines of code.
    nums1[m:] = nums2[:n]

Log in to reply

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