【动态规划】LeetCode 63. Unique Paths II
程序员文章站
2022-07-12 12:39:35
...
LeetCode 63. Unique Paths II
Solution1:我的答案
在哪里做过这题?
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
if (!m) return 0;
int n = obstacleGrid[0].size();
vector<vector<int> > dp(m, vector<int> (n, 1));
for (int i = 0; i < m;) {
if (obstacleGrid[i][0]) {
for (; i < m; i++)
dp[i][0] = 0;
}
i++;
}
for (int i = 0; i < n;) {
if (obstacleGrid[0][i]) {
for (; i < n; i++)
dp[0][i] = 0;
}
i++;
}
for (int i = 1; i < m; ++i)
for (int j = 1; j < n; ++j) {
if (obstacleGrid[i][j])
dp[i][j] = 0;
else
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
return dp[m - 1][n - 1];
}
};
上一篇: 621. 任务调度器
推荐阅读
-
63. Unique Paths II 动态规划
-
63. Unique Paths II 动态规划
-
【leetcode】Unique Paths II(动态规划)
-
LeetCode 63. Unique Paths II(动态规划)
-
[LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
-
动态规划----unique paths
-
【LeetCode62 Unique Paths】动态规划计算路径
-
【动态规划】LeetCode 63. Unique Paths II
-
牛客网_leetcode_unique-paths-ii(动态规划)
-
Unique Paths(动态规划求解)