Get wrong answer of my python but can't find what is mistaken


  • 0
    J

    I write the following code but get the wrong answer.
    [1],[2]
    expectd: [1,2]
    wrong:[1,0]

    But the code run fine in my linux enviroment. What is mistaken?

     def findInsert(self,A,begin,end,mB):
        if end>=begin:
            middle=(begin+end)/2
            if A[middle]==mB:
                return middle
            elif A[middle]<mB:
                begin=middle+1
                return self.findInsert(A,begin,end,mB)
            else:
                end=middle-1
                return self.findInsert(A,begin,end,mB)
        else:
            return begin      
    
    
    
    def merge(self, A, m, B, n):
        
        if m==0 and n==0:
            return
        
        if m==0 or n==0:
            j=0
            for i in range(m,m+n):
                A[i]=B[j]
                j=j+1
            return 
        
            
        begin=0
        end=m-1
        for i in range(0,n):
            indexA=self.findInsert(A,begin,end,B[i])
            if indexA==end+1:
                while i<n:
                    A.append(B[i])
                    i=i+1
                return
            else:
                A.insert(indexA,B[i])
                begin=indexA+1
                end=end+1

  • 1
    K

    It seems the problem is A.append(B[i]), which is adding new element at index 2.


Log in to reply
 

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