当前位置:网站首页>力扣: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();
}
};
边栏推荐
- 8. Haproxy builds a web cluster
- Programming hodgepodge (4)
- manipulation of file contents
- 触觉智能分享-SSD20X实现升级显示进度条
- System design. How to design a spike system (full version transfer)
- Dynamic programming of the division of numbers
- 【一步到位】Jenkins的安装、部署、启动(完整教程)
- System design. Seckill system
- 应届生软件测试薪资大概多少?
- Introduction and application of go module
猜你喜欢

如何将 DevSecOps 引入企业?

获取单选框选中内容

7. The principle description of LVS load balancing cluster

Simple operation of the file system

Tactile intelligent sharing - SSD20X realizes upgrade display progress bar

ADC噪声全面分析 -03- 利用噪声分析进行实际设计

转:管理是对可能性的热爱,管理者要有闯进未知的勇气

在被面试官说了无数次后,终于潜下心来整理了一下JVM的类加载器

Mini program + e-commerce, fun new retail

【流程图】
随机推荐
【SemiDrive源码分析】【MailBox核间通信】47 - 分析RPMSG_IPCC_RPC 方式 单次传输的极限大小 及 极限带宽测试
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)
C专家编程 第5章 对链接的思考 5.3 函数库链接的5个特殊秘密
2023年PMP考试会用新版教材吗?回复来了!
What are the functions of mall App development?
使用Patroni回调脚本绑定VIP的坑
flink cdc一启动,源端Oracle那台服务器的CPU就飙升到80%以上,会是啥原因呢?
腾讯136道高级岗面试题:多线程+算法+Redis+JVM
JVM Notes
转:管理是对可能性的热爱,管理者要有闯进未知的勇气
使用Loadrunner进行性能测试
There is an 8 hour difference between the docker installation of mysql and the host.
Mini program + e-commerce, fun new retail
应届生软件测试薪资大概多少?
go module的介绍与应用
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
如何打造一篇优秀的简历
Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
【流程图】
C Expert Programming Chapter 5 Thinking about Linking 5.2 Advantages of Dynamic Linking