题目要求

Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the grid.For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.[[0,0,0],[0,1,0],[0,0,0]
]
The total number of unique paths is 2.Note: m and n will be at most 100.

这是Unique Path题目系列。关于Unique Path I请参考我的这篇博客。相比于I,这里添加的需求是说,某些节点上存在路障。存在路障的节点会在数组中被标记为1。请问从起点到终点有多少条独立路径。

思路和代码

Unique Path I的思路基础上,我们可以知道,如果某个节点上存在路障,那么任何从该节点前往终点的路径都将不存在。也就是说,该节点的路径数为0。在此基础上,我们可以知道,如果该节点为路障,则该节点路径数为0,否则该节点的路径数等于左侧节点路径数和上方节点路径数的和。代码如下:

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {int row = obstacleGrid.length;if(row==0){return 0;}int column = obstacleGrid[0].length;int path = obstacleGrid[0][0] == 1 ? 0 : 1;for(int i = 1 ; i<row ; i++){if(obstacleGrid[i][0] == 1){path = 0;}obstacleGrid[i][0] = path;}path = 1;for(int i = 0 ; i<column ; i++){if(obstacleGrid[0][i] == 1){path = 0;}obstacleGrid[0][i] = path;}for(int i = 1 ; i<row ; i++){for(int j = 1 ; j<column ; j++){if(obstacleGrid[i][j] == 1){obstacleGrid[i][j] = 0;}else{obstacleGrid[i][j] = obstacleGrid[i][j-1] + obstacleGrid[i-1][j];}}}return obstacleGrid[row-1][column-1];}


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

leetcode63. 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. Unique Paths II leetcode java

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

  5. 63. Unique Paths II

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

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

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

  7. [LeetCode][Java] Unique Paths II

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

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

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

  9. LeetCode 63. Unique Paths II

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

最新文章

  1. PREDATOR: 低重叠三维点云的配准方法(CVPR2021)
  2. 深度学习与机器学习的思考
  3. Android程序ToDoList增加配置项页面
  4. word2vec python实现_教程 | 在Python和TensorFlow上构建Word2Vec词嵌入模型
  5. 从xib 创建 collectionViewCell
  6. 浏览器的渲染原理简介
  7. matlab radn,如何用matlab编写randn函数?
  8. springboot 打包_springboot打包-体积太大怎么办
  9. Linux的性能故障的含义,Linux排查性能故障的方法
  10. 发动机压缩比怎么计算公式_怎么判断发动机有积碳,发动机积碳多的症状有哪些...
  11. linux删除文件_Linux删除文件夹用什么命令
  12. display: inline-block;水平居中
  13. Mybatis面试题-日更
  14. hive 2.1.1 mysql_Hive2.1.1集群搭建
  15. DBGridEh全部属性设置详解
  16. html5作品分析报告,性能报告之HTML5 性能测试报告
  17. session和cookies
  18. Android 第一行代码(第2版).pdf 网盘
  19. 腾讯广告算法大赛(即腾讯社交广告算法大赛)
  20. 爬取虎扑nba球员得分榜信息并存储至MongoDB数据库

热门文章

  1. Xilinx® 7 series FPGAs CLBs专题介绍(一)
  2. 通信系统之信道(三)
  3. 性能优化技巧 - 内存关联计算
  4. 深度学习与计算机视觉:基于Python的神经网络的实现
  5. bind、call、apply
  6. BZOJ1011 莫比乌斯反演(基础题
  7. MongoDB 和 NoSQL简介
  8. 中间件事务码R3AC1里Block Size的含义
  9. piwik的安装与配置
  10. Docker - Tips