Movatterモバイル変換


[0]ホーム

URL:


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

Commitcd4be47

Browse files
Merge pull requestyoungyangyang04#439 from EnzoSeason/leetcode-63
update 63. 不同路径 II:提供一维dp数组的Python解法
2 parentsa04dd4e +24e7994 commitcd4be47

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
@@ -232,6 +232,38 @@ class Solution:
232232
return dp[-1][-1]
233233
```
234234

235+
```python
236+
classSolution:
237+
"""
238+
使用一维dp数组
239+
"""
240+
241+
defuniquePathsWithObstacles(self,obstacleGrid: List[List[int]]) ->int:
242+
m, n=len(obstacleGrid),len(obstacleGrid[0])
243+
244+
# 初始化dp数组
245+
# 该数组缓存当前行
246+
curr= [0]* n
247+
for jinrange(n):
248+
if obstacleGrid[0][j]==1:
249+
break
250+
curr[j]=1
251+
252+
for iinrange(1, m):# 从第二行开始
253+
for jinrange(n):# 从第一列开始,因为第一列可能有障碍物
254+
# 有障碍物处无法通行,状态就设成0
255+
if obstacleGrid[i][j]==1:
256+
curr[j]=0
257+
elif j>0:
258+
# 等价于
259+
# dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
260+
curr[j]= curr[j]+ curr[j-1]
261+
# 隐含的状态更新
262+
# dp[i][0] = dp[i - 1][0]
263+
264+
return curr[n-1]
265+
```
266+
235267

236268
Go:
237269

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp