Annoying input: [], [1]


  • 5
    M

    I got accepted using following code:

    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):
        i=0
        j=0
        while i<len(A) and j<n:
            if A[i]>=B[j]:
                A.insert(i,B[j])
                j+=1
            i+=1
        while j<n:
            A.insert(m+j,B[j])
            j+=1
    

    But I got wrong answer using following code on the input: [], [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):
        i=0
        j=0
        while i<len(A) and j<n:
            if A[i]>=B[j]:
                A.insert(i,B[j])
                j+=1
            i+=1
        while j<n:
            A.append(B[j])
            j+=1
    

    or:

    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):
        i=0
        j=0
        while i<len(A) and j<n:
            if A[i]>=B[j]:
                A.insert(i,B[j])
                j+=1
            i+=1
        while j<n:
            A.insert(len(A),B[j])
            j+=1
    

    or:

    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):
        i=0
        j=0
        while i<len(A) and j<n:
            if A[i]>=B[j]:
                A.insert(i,B[j])
                j+=1
            i+=1
        A+=B[j:]
    

    It says my output is [0], however, the expected output is [1], which is the same as what I get on my computer. Just can not figure out the reason. Could anyone help me? Thank you!


  • 0
    V

    I got the same problem...


  • 0
    Y

    me too...have no idea what went wrong


  • 1
    S

    I think the reason is the array A in the test case is not an empty array since the question mentioned its size is at least m+n. Thus, when you use .append or len(A), you get the real size of A and append to its end.


  • 0
    Y

    i got the same problem, really annoying........................................


Log in to reply
 

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