My python solution shows wrong on OJ with input [][1], but right in my computer


  • 1
    S

    Can someone help? Thanks.

    Input: [], [1]

    Output: [0] (Output is [1] in my computer)

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

  • 0
    B

    try this:

    A = []
    B = [1]
    merge(A,0,B,1)
    print A
    

    Don't return and print

    And try this though ugly:

    def merge(self, A, m, B, n):
            i = 0
            j = 0
    
            while i < m and j < n:
                if A[i] < B[j]:
                    i += 1
                else:
                    A.insert(i,B[j])
                    i += 1
                    m += 1
                    j += 1
            while j < n:
                A[i] = B[j]
                i += 1
                j += 1

  • 0
    S

    Thanks. I tried your solution. A[i] = B[j] need to be changed into A.insert(i,B[j]) to pass OJ


Log in to reply
 

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