Here's my code:
def uniquePathsWithObstacles(self, obstacleGrid): if not obstacleGrid: return 0 m,n = len(obstacleGrid),len(obstacleGrid) mat = [*(n+1)]*(m+1) mat[m-1][n] = 1 for i in reversed(range(m)): for j in reversed(range(n)): mat[i][j] = 0 if obstacleGrid[i][j] == 1 else mat[i][j+1]+mat[i+1][j] return mat
When input is [,] the output is 2, which should be 1 as correct answer.
I can't find out why and how to fix it. Thanks if anyone can help me fix this problem!
Try initialize the matrix using
[[0 for col in range(n+1)] for row in range(m+1)]
which makes m+1 copies of the reference of the same 1D array.
Thank you, that works! But could you explain what's the difference between these two?
Because I tried:
lis1 = [[0 for col in range(n+1)] for row in range(m+1)] lis2 = [*(n+1)]*(m+1) print lis1 == lis2
the result is True, so I don't see any difference between them.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.