Is there a bug in my code or test case?

  • 0

    I keep failing the case for nums1 = [0], m = 0, nums2 = [1], n = 1. Solution should be [1]. I even tried to print out the list at the end and it is [1]. However, OJ keeps thinking the output is [0].

       class Solution(object):
            def merge(self, nums1, m, nums2, n):
                :type nums1: List[int]
                :type m: int
                :type nums2: List[int]
                :type n: int
                :rtype: void Do not return anything, modify nums1 in-place instead.
                nums1 = nums1[:m]
                nums2 = nums2[:n]
                one = 0 #the index of the next element we want to examine in nums1
                two = 0 #the index of the next element we want to examine in nums2
                total = 0 #the index of the next comparison between num1 and num2
                while one < m and two < n and total < m + n:
                    if nums1[one] <= nums2[two]:
                        one += 1
                        nums1.insert(total, nums2[two])
                        one += 1 #don't forget this because you insert one element, so you shift everything by 1
                        two += 1
                    total += 1
                if two <= n:
                    nums1[total:] = nums2[two:n]

  • 1

    The first thing you do is

    nums1 = nums1[:m]

    and BAM!! you have right away lost all access to the given list, the one that you're supposed to modify. So at that point it's already impossible to finish the task.

  • 0

    Thanks for your clarification!!

Log in to reply

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