Skip to content

Commit

Permalink
Update 0063.不同路径II.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghongcheng authored Jun 12, 2023
1 parent 50ed104 commit 5527410
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions problems/0063.不同路径II.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5527410

Please sign in to comment.