Share python solution, and ask a related question


  • 0
    Z

    The solution is quite straightforward:

    def uniquePathsWithObstacles(self, obstacleGrid):
        count = [[0]*(len(obstacleGrid[0])+1)]*(len(obstacleGrid)+1)
        count[0][1]= 1
        for i in range(1,len(count)):
            for j in range(1,len(count[0])):
                if obstacleGrid[i-1][j-1] == 1: count[i][j] = 0
                else: count[i][j] = count[i-1][j] + count[i][j-1]
        return count[-1][-1]
    

    My question is about a test case when I run python on my laptop, basically I give an input, I want to change value 1 to 0, and 0 to 1:

    input = [[0,0,1,0],[0,1,0,0],[0,0,0,0]]
    count = [[1]*len(input[0])]*len(input)
    
    for i in range(len(input)):
        for j in range(len(input[0])):
            count[i][j] = count[i][j] - input[i][j]       # A
            # if input[i][j]: count[i][j] = 0               # B
            # count[i][j] = abs(count[i][j]-input[i][j])    # C
    
    print(count)
    

    However, neither of the method (A,B,C) is correct? My output is always

    [[1, 0, 0, 1], [1, 0, 0, 1], [1, 0, 0, 1]]
    

    Can any one help to explain? Thanks!


  • 0
    Z

    I figured it out...

    I should have used

    count = [[1 for x in range(len(input[0]))] for x in range(len(input))]
    

    rather than

    count = [[1]*len(input[0])]*len(input)

Log in to reply
 

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