当前位置:网站首页>力扣:63. 不同路径 II
力扣:63. 不同路径 II
2022-08-04 05:14:00 【empty__barrel】
力扣:63. 不同路径 II
题目:
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish”)。
现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?
网格中的障碍物和空位置分别用 1 和 0 来表示。
普通代码:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
int dp[m][n];
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
if(obstacleGrid[i][j] == 1){
dp[i][j] = 0;
continue;
}
if(i == 0 && j == 0){
dp[i][j] = 1;
continue;
}
int a = i-1 < 0?0:dp[i-1][j];
int b = j-1 < 0?0:dp[i][j-1];
dp[i][j] = a+b;
}
}
return dp[m-1][n-1];
}
};
代码:一维数组实现
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if (obstacleGrid[0][0] == 1)
return 0;
vector<int> dp(obstacleGrid[0].size());
for (int j = 0; j < dp.size(); ++j)
if (obstacleGrid[0][j] == 1)
dp[j] = 0;
else if (j == 0)
dp[j] = 1;
else
dp[j] = dp[j-1];
for (int i = 1; i < obstacleGrid.size(); ++i)
for (int j = 0; j < dp.size(); ++j){
if (obstacleGrid[i][j] == 1)
dp[j] = 0;
else if (j != 0)
dp[j] = dp[j] + dp[j-1];
}
return dp.back();
}
};
边栏推荐
- 少年成就黑客,需要这些技能
- 【21天学习挑战赛】直接插入排序
- How to open a CITIC Securities online account?is it safe?
- Jenkins export and import Job Pipeline
- share总结
- 一个对象引用的思考
- C Expert Programming Chapter 5 Thinking about Linking 5.1 Libraries, Linking and Loading
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.4 Matching declarations to definitions
- 结构体函数练习
- Chapter 5 C programming expert thinking 5.4 alert Interpositioning of links
猜你喜欢
随机推荐
符号表
[Skill] Using Sentinel to achieve priority processing of requests
Mini program + e-commerce, fun new retail
3000 words, is take you understand machine learning!
Use Patroni callback script to bind VIP pit
字节最爱问的智力题,你会几道?
【21天学习挑战赛】直接插入排序
信息学奥赛一本通 1312:【例3.4】昆虫繁殖
【一步到位】Jenkins的安装、部署、启动(完整教程)
See how DevExpress enriches chart styles and how it empowers fund companies to innovate their business
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)
el-Select selector bottom fixed
使用Patroni回调脚本绑定VIP的坑
For Qixi Festival, I made a confession envelope with code
System design. Seckill system
详解八大排序
渗透测试(PenTest)基础指南
[Cocos] cc.sys.browserType可能的属性
7. The principle description of LVS load balancing cluster
深度学习21天——卷积神经网络(CNN):实现mnist手写数字识别(第1天)









