Unique Paths II
注意一个容易犯的错误:判断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];} };
Unique Paths II相关推荐
- [Lintcode]115. Unique Paths II/[Leetcode]63. Unique Paths II
115. Unique Paths II/63. Unique Paths II 本题难度: Easy/Medium Topic: Dynamic Programming Description Fo ...
- 63. Unique Paths II and 64. Minimum Path Sum
文章目录 1 63 Unique Paths II 1.1 题目描述 1.2 动态规划解决 2 64. Minimum Path Sum 2.1 题目理解 2.2 动态规划 这一遍刷dp的题目就很轻松 ...
- 【动态规划】LeetCode 63. Unique Paths II
LeetCode 63. Unique Paths II Solution1:我的答案 在哪里做过这题? class Solution { public:int uniquePathsWithObst ...
- leetcode63. Unique Paths II
题目要求 Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. H ...
- Unique Paths II leetcode java
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 63. Unique Paths II
和Unique paths是一样的 1 public int uniquePathsWithObstacles(int[][] obstacleGrid) { 2 if(obstacleGrid == ...
- [LeetCode]--63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode][Java] Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- C#LeetCode刷题之#63-不同路径 II(Unique Paths II)
目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3682 访问. 一个机器人位于一个 m x ...
- LeetCode 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
最新文章
- ​数据分析必读干货:简单而实用的3大分析方法
- pentaho中Invalid byte 3 of 3-byte UTF-8 sequence的解决方法
- 一个MySQL时间戳精度引发的血案
- Master DNS服务的搭建
- PotPlayer安装与配置
- 阶乘因式分解(一) -- ACM解决方案
- 爱因斯坦是怎样学习物理的?
- [C#] 如何分析stackoverflow等clr错误
- enum枚举类型的范例
- Playing Atari with Deep Reinforcement Learning 中文 讲解
- 小说app源码,uni-app跨平台框架开发,一套代码双端运行,无差别
- linux 部署应用服务器,(小白指南)在 Linux 服务器上安装 Nodejs、Nginx 以及部署 Web 应用...
- 使用Apache架设代理服务器
- C语言理论小学案例试讲,C语言试讲演示文稿.ppt
- Python3简单爬虫:爬取猫眼评分top100电影
- 第五章.系统安全分析与设计
- android 汉字 unicode编码,Android解析UniCode编码
- 武汉大学计算机学院考研心得,2018年武汉大学计算机学院考研复试经验分享
- oracle的购买价格研究
- 汉江师范数学与计算机科学院宿舍,汉江师范学院宿舍条件,宿舍几人间环境好不好(图片)...