Update 0063.不同路径II.md

This commit is contained in:
jianghongcheng
2023-06-12 02:59:20 -05:00
committed by GitHub
parent 50ed10475d
commit 5527410f89

View File

@ -397,7 +397,39 @@ class Solution:
```
动态规划(版本五)
```python
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid):
if obstacleGrid[0][0] == 1:
return 0
m, n = len(obstacleGrid), len(obstacleGrid[0])
dp = [0] * n # 创建一个一维列表用于存储路径数
# 初始化第一行的路径数
for j in range(n):
if obstacleGrid[0][j] == 1:
break
dp[j] = 1
# 计算其他行的路径数
for i in range(1, m):
if obstacleGrid[i][0] == 1:
dp[0] = 0
for j in range(1, n):
if obstacleGrid[i][j] == 1:
dp[j] = 0
continue
dp[j] += dp[j - 1]
return dp[-1] # 返回最后一个元素,即终点的路径数
```
### Go
```go