63. Unique Paths II 动态规划
程序员文章站
2022-07-12 12:59:10
...
跟Unique Paths一样,也是使用动态规划的问题。
解决此题的方法也和之前一样,先建立一个与目标矩阵相同尺寸的矩阵,然后
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
m = len(obstacleGrid); n = len(obstacleGrid[0])
res = [[0 for i in range(n)] for j in range(m)]
for i in range(m):
if obstacleGrid[i][0] == 0:
res[i][0] = 1
else:
res[i][0] = 0
break
for i in range(n):
if obstacleGrid[0][i] == 0:
res[0][i] = 1
else:
res[0][i] = 0
break
for i in range(1, m):
for j in range(1, n):
if obstacleGrid[i][j] == 1: res[i][j] = 0
else:
res[i][j] = res[i-1][j] + res[i][j-1]
return res[m-1][n-1]
推荐阅读
-
63. Unique Paths II 动态规划
-
63. Unique Paths II 动态规划
-
【leetcode】Unique Paths II(动态规划)
-
LeetCode 63. Unique Paths II(动态规划)
-
[LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
-
动态规划----unique paths
-
【LeetCode62 Unique Paths】动态规划计算路径
-
【动态规划】LeetCode 63. Unique Paths II
-
牛客网_leetcode_unique-paths-ii(动态规划)
-
Unique Paths(动态规划求解)