How does this code will fail with same procedure as right code?


  • 0
    J
        result = [[1],[1,1]]
        curr = [1,1]
        index = []
        if numRows == 0:
            return []
        if numRows == 1:
            return [[1]]
        if numRows == 2:
            return result
        for i in range(numRows-2):
            index = [1]
            while len(curr)>1:
                x = curr.pop(0)
                index.append(x+curr[0])
            index.append(1)
            curr = index
            result.append(index)
            
        return result
    

    It very tricky that I found this code will change the result list while running curr change, as marked. I only pop the element from the curr list, but result list will also be changed. I don't get the reason, can somebody explain ? You can test this code, and will get very funny answer.


Log in to reply
 

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