Why wrong answer?


  • 0
    B

    My code is:

    def merge(self, A, m, B, n):
        i=0
        j=0
        A=A+B
        while i<m:
            if j>-n:
                while A[m-j]<A[m-j-1] and m-j-1>=0:
                    t=A[m-j]
                    A[m-j]=A[m-j-1]
                    A[m-j-1]=t
                    j=j+1
            i=i+1
            j=-i
    

    Input: [ ],[1]

    Output: [0]

    Expected: [1]


  • 0
    S

    I believe this line A=A+B should be instead of m=m+n, or something like that.

    Since m indicates the original length of A, n indicates length of B, also as problem describes that

    You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B.


Log in to reply
 

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