Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5527410

Browse files
Update 0063.不同路径II.md
1 parent 50ed104 commit 5527410

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

‎problems/0063.不同路径II.md‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,39 @@ class Solution:
397397

398398
```
399399

400+
动态规划(版本五)
400401

402+
```python
403+
class Solution:
404+
def uniquePathsWithObstacles(self, obstacleGrid):
405+
if obstacleGrid[0][0] == 1:
406+
return 0
407+
408+
m, n = len(obstacleGrid), len(obstacleGrid[0])
409+
410+
dp = [0] * n # 创建一个一维列表用于存储路径数
411+
412+
# 初始化第一行的路径数
413+
for j in range(n):
414+
if obstacleGrid[0][j] == 1:
415+
break
416+
dp[j] = 1
417+
418+
# 计算其他行的路径数
419+
for i in range(1, m):
420+
if obstacleGrid[i][0] == 1:
421+
dp[0] = 0
422+
for j in range(1, n):
423+
if obstacleGrid[i][j] == 1:
424+
dp[j] = 0
425+
continue
426+
427+
dp[j] += dp[j - 1]
428+
429+
return dp[-1] # 返回最后一个元素,即终点的路径数
430+
431+
432+
```
401433
### Go
402434

403435
```go

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /