当前位置:网站首页>力扣: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();
}
};
边栏推荐
- How to open a CITIC Securities online account?is it safe?
- 2022软件测试面试题 最新字节跳动50道真题面试题 刷完已拿下15k 附讲解+答疑
- How to keep the source code confidential in the development under the burning scenario
- 数的划分之动态规划
- Use Patroni callback script to bind VIP pit
- 心余力绌:企业面临的软件供应链安全困境
- 在被面试官说了无数次后,终于潜下心来整理了一下JVM的类加载器
- redis中常见的面试题
- 2022 software test interview questions The latest ByteDance 50 real interview questions, 15k have been won after brushing, with explanation + Q&A
- Introduction and application of go module
猜你喜欢

深度学习环境配置

See how DevExpress enriches chart styles and how it empowers fund companies to innovate their business

Performance testing with Loadrunner

Converts XML tags to TXT format (voc conversion for yolo convenient training)

10 Convolutional Neural Networks for Deep Learning 3

一个对象引用的思考

Teenage Achievement Hackers Need These Skills

深度学习21天——准备(环境配置)

Shocked, 99.9% of the students didn't really understand the immutability of strings

【一步到位】Jenkins的安装、部署、启动(完整教程)
随机推荐
Use Patroni callback script to bind VIP pit
C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.3 What is a Declaration and What is a Definition
DataTable使用Linq进行分组汇总,将Linq结果集转化为DataTable
21 days learning challenge 】 【 sequential search
QT 如何识别文件的编码格式
ADC噪声全面分析 -03- 利用噪声分析进行实际设计
C专家编程 第5章 对链接的思考 5.2 动态链接的优点
【C语言进阶】程序环境和预处理
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.2 我的代码为什么无法运行
el-Select 选择器 底部固定
8大软件供应链攻击事件概述
商城系统APP如何开发 都有哪些步骤
What are the functions of mall App development?
Simple operation of the file system
Landing, the IFC, GFC, FFC concept, layout rules, forming method, use is analysed
企业需要知道的5个 IAM 最佳实践
C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.2 Why does my code not work
【21天学习挑战赛】顺序查找
Bolb analysis of image processing (1)
See how DevExpress enriches chart styles and how it empowers fund companies to innovate their business