注意一个容易犯的错误:判断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相关推荐

  1. [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 ...

  2. 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的题目就很轻松 ...

  3. 【动态规划】LeetCode 63. Unique Paths II

    LeetCode 63. Unique Paths II Solution1:我的答案 在哪里做过这题? class Solution { public:int uniquePathsWithObst ...

  4. leetcode63. Unique Paths II

    题目要求 Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. H ...

  5. Unique Paths II leetcode java

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  6. 63. Unique Paths II

    和Unique paths是一样的 1 public int uniquePathsWithObstacles(int[][] obstacleGrid) { 2 if(obstacleGrid == ...

  7. [LeetCode]--63. Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  8. [LeetCode][Java] Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  9. C#LeetCode刷题之#63-不同路径 II​​​​​​​(Unique Paths II)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3682 访问. 一个机器人位于一个 m x ...

  10. LeetCode 63. Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

最新文章

  1. ​数据分析必读干货:简单而实用的3大分析方法
  2. pentaho中Invalid byte 3 of 3-byte UTF-8 sequence的解决方法
  3. 一个MySQL时间戳精度引发的血案
  4. Master DNS服务的搭建
  5. PotPlayer安装与配置
  6. 阶乘因式分解(一) -- ACM解决方案
  7. 爱因斯坦是怎样学习物理的?
  8. [C#] 如何分析stackoverflow等clr错误
  9. enum枚举类型的范例
  10. Playing Atari with Deep Reinforcement Learning 中文 讲解
  11. 小说app源码,uni-app跨平台框架开发,一套代码双端运行,无差别
  12. linux 部署应用服务器,(小白指南)在 Linux 服务器上安装 Nodejs、Nginx 以及部署 Web 应用...
  13. 使用Apache架设代理服务器
  14. C语言理论小学案例试讲,C语言试讲演示文稿.ppt
  15. Python3简单爬虫:爬取猫眼评分top100电影
  16. 第五章.系统安全分析与设计
  17. android 汉字 unicode编码,Android解析UniCode编码
  18. 武汉大学计算机学院考研心得,2018年武汉大学计算机学院考研复试经验分享
  19. oracle的购买价格研究
  20. 汉江师范数学与计算机科学院宿舍,汉江师范学院宿舍条件,宿舍几人间环境好不好(图片)...

热门文章

  1. Android OpenGL使用GLSurfaceView预览视频
  2. 从内存溢出看Java 环境中的内存结构
  3. ECSHOP学习笔记
  4. mogileFS 分布式存储-安装手记
  5. ASP.NET 配置节架构
  6. VS.Net中程序集的Debug版本和Release版本的区别
  7. Combox控件下拉选择不同值触发的动作响应
  8. C++拾趣——STL容器的插入、删除、遍历和查找操作性能对比(Windows VirtualStudio)——遍历和删除
  9. Python3中迭代器介绍
  10. OpenCV中cvBlobsLib的编译与使用