From 5527410f890ecfccdb017ce5aa5fc902328ab922 Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Mon, 12 Jun 2023 02:59:20 -0500 Subject: [PATCH] =?UTF-8?q?Update=200063.=E4=B8=8D=E5=90=8C=E8=B7=AF?= =?UTF-8?q?=E5=BE=84II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0063.不同路径II.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0063.不同路径II.md b/problems/0063.不同路径II.md index 7b5b44f0..cb305b41 100644 --- a/problems/0063.不同路径II.md +++ b/problems/0063.不同路径II.md @@ -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