1 class Solution:
 2     def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
 3         m = len(obstacleGrid)
 4         n = len(obstacleGrid[0])
 5         dp = [[0 for _ in range(n)]for _ in range(m)]
 6         for j in range(n):
 7             if obstacleGrid[0][j] == 0:
 8                 dp[0][j] = 1
 9             else:
10                 break
11         for i in range(m):
12             if obstacleGrid[i][0] == 0:
13                 dp[i][0] = 1
14             else:
15                 break
16         for i in range(1,m):
17             for j in range(1,n):
18                 if obstacleGrid[i][j] == 0:
19                     dp[i][j] = dp[i-1][j] + dp[i][j-1]
20                 else:
21                     dp[i][j] = 0
22         return dp[m-1][n-1]

leetcode62的基础上,增加对障碍物的判断。

posted on 2019-12-10 22:31  Sempron2800+  阅读(156)  评论(0编辑  收藏  举报