Unique Paths II

news/2024/7/5 10:34:22

注意一个容易犯的错误:判断obstacleGrid是否为1时,else那部分不能少。因为如果不加,就会默认把那些值设置为0。

class Solution {
public:int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {int height = obstacleGrid.size();int width = obstacleGrid[0].size();vector<vector<int>> result(height,vector<int>(width));if(obstacleGrid[0][0] == 1 || obstacleGrid[height-1][width-1] == 1)return 0;for(int i = 0;i < height;i++){for(int j = 0;j < width;j++){if(obstacleGrid[i][j] == 1)result[i][j] = 0;elseresult[i][j] = -1;}}result[0][0] = 1;for(int i = 0;i < height;i++){for(int j = 0;j < width;j++){if(i == 0 && j == 0)continue;if(result[i][j] == 0)continue;if(i != 0 && j != 0)result[i][j] = result[i][j-1] + result[i-1][j];else if(i == 0)result[i][j] = result[i][j-1];elseresult[i][j] = result[i-1][j];}}return result[height-1][width-1];}
};

 初始化第一行第一列

class Solution {
public:int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {int length = obstacleGrid.size();if(length <= 0)return 0;int width = obstacleGrid[0].size();vector<vector<int>> result(length,vector<int>(width));if(obstacleGrid[0][0] == 1)result[0][0] = 0;elseresult[0][0] = 1;for(int i = 1;i < length;i++){if(obstacleGrid[i][0] == 1 || result[i-1][0] == 0)result[i][0] = 0;elseresult[i][0] = 1;}for(int j = 1;j < width;j++){if(obstacleGrid[0][j] == 1 || result[0][j-1] == 0)result[0][j] = 0;elseresult[0][j] = 1;}for(int i = 1;i < length;i++){for(int j = 1;j < width;j++){if(obstacleGrid[i][j] == 1)result[i][j] = 0;//else if(result[i-1][j] == 0)      这些不注释掉也能跑出正确结果//result[i][j] = result[i][j-1];//else if(result[i][j-1] == 0)//result[i][j] = result[i-1][j];elseresult[i][j] = result[i][j-1] + result[i-1][j];}}return result[length-1][width-1];}
};

 


http://lihuaxi.xjx100.cn/news/239670.html

相关文章

uber_Uber是如何制成的

uberby Dmytro Brovkin由Dmytro Brovkin Uber是如何制成的 (How Uber was made) Uber has transformed the world. Indeed, its inconceivable to think of a world without the convenience of the innovative ride sharing service. Tracing its origins in a market which …

2021年甘肃省副高考试成绩查询,2021年甘肃卫生资格考试成绩查询-中国卫生人才网...

国家卫生资格考试网为您发布 2021年甘肃卫生资格考试成绩查询-中国卫生人才网&#xff0c;同步中国卫生人才网信息&#xff1a;2021甘肃卫生资格成绩查询。更多关于卫生资格成绩,卫生资格考试,2021卫生资格考试,国家卫生资格成绩查询的信息内容&#xff0c;请关注国家卫生资格考…

怎么写shell脚本才能不耍流氓?

1、不记录日志的 SHELL 脚本就是耍流氓&#xff01; 我们经常在工作中会遇到一个苦恼的事情&#xff0c;一个 Shell 脚本到底干了什么&#xff0c;什么时候开始执行&#xff0c;什么时候结束的。尤其是数据库备份&#xff0c;我们想知道我们的 MySQL 数据库备份时间。所以给脚本…

es6 ... 添加属性_如何在10分钟内免费将HTTPS添加到您的网站,以及为什么您现在不止需要这样做......

es6 ... 添加属性by Ayo Isaiah通过Ayo Isaiah 如何在10分钟内免费将HTTPS添加到您的网站&#xff0c;以及为什么现在比以往更需要这样做 (How to add HTTPS to your website for free in 10 minutes, and why you need to do this now more than ever) Last week, Google ann…

oracle中的exists 和 not exists 用法详解

from&#xff1a;http://blog.sina.com.cn/s/blog_601d1ce30100cyrb.htmloracle中的exists 和 not exists 用法详解 (2009-05-14 16:58:18) 有两个简单例子&#xff0c;以说明 “exists”和“in”的效率问题1) select * from T1 where exists(select 1 from T2 where T1.aT2.a)…

台式计算机刚换的显示屏怎么设置,台式机怎么样切换显示器

我的台式机是双显示器&#xff0c;该怎么样去切换呢?下面由小编给你做出详细的台式机切换显示器方法介绍!希望对你有帮助!台式机切换显示器方法一&#xff1a;需要用到的硬件&#xff1a;1、支持双视频信号输出的显卡。如一块显卡支持VGA输出&#xff0c;又同时支持DVI输出。2…

CCIE-MPLS基础篇-实验手册

又一部前期JUSTECH&#xff08;南京捷式泰&#xff09;工程师职业发展系列丛书完整拷贝。 MPLS&#xff08;Multi-Protocol Label Switching&#xff09; 目录 1&#xff1a;MPLS 基础实验.... 3 1.1实验拓扑... 3 1.2实验需求&#xff1a;... 3 1.3实验步骤... 3 1.4校验…

电子界卡组构建2019_2018–2019年构建现代Android应用程序的路线图

电子界卡组构建2019Kriptofolio应用程序系列—简介 (Kriptofolio app series — Introduction) Welcome to this series of blog posts where I will be creating a modern Android app. I will use the best tools and practices available in the year 2018–2019. I am doin…