题目:

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 Paths》,现给出这样一题目:

假设在格子中加入一些障碍,会出现多少存在且唯一的不同路径呢?

障碍和空白格子分别被标记为1 and 0 .

比方一个3x3的格子中的中间存在一个障碍,例如以下所看到的:

[[0,0,0],[0,1,0],[0,0,0]
]

总的路径数为2.

算法分析:

思路与题目《Unique Paths》类似,不同之处为:

初始化边界上行和列时,出现障碍。后面路径数dp的都是0

中间的格子出现障碍时,该格子dp表示的路径数直接填0

AC代码:

public class Solution
{public int uniquePathsWithObstacles(int[][] obstacleGrid) {if(obstacleGrid==null||obstacleGrid.length==0)return 0;int m = obstacleGrid.length;int n = obstacleGrid[0].length;int [][] dp = new int[m][n];for(int i = 0; i < m; i++){if(obstacleGrid[i][0]!=1)dp[i][0] = 1;else break;}for(int j = 0; j < n; j++){if(obstacleGrid[0][j]!=1)dp[0][j] = 1;else break;}for(int i = 1; i < m; i++){for(int j = 1; j< n; j++){if(obstacleGrid[i][j]!=1)dp[i][j] = dp[i-1][j] + dp[i][j-1];elsedp[i][j]=0;}}return dp[m-1][n-1];}
}

[LeetCode][Java] Unique Paths II相关推荐

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

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

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

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

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

  4. LeetCode 63. Unique Paths II

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

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

  6. 【动态规划】LeetCode 62. Unique Paths

    LeetCode 62. Unique Paths Solution1:我的未能AC的答案 递归超时了!!! class Solution { public:int uniquePaths(int m ...

  7. Unique Paths II leetcode java

    题目: 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. leetcode63. Unique Paths II

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

最新文章

  1. 超越ResNet:南开提出Res2Net,不增计算负载,性能全面升级!
  2. FastReport3.20简易安装方法。
  3. listview mysql查询_Sqlite 数据库分页查询(ListView分页显示数据)
  4. [javascript] Date 时间精确到天
  5. 广东省计算机大赛设计什么时候,2017年广东省大学生计算机设计大赛
  6. 从编译安装Keepalived 到 配置 负载均衡(LVS-DR)
  7. 剧本翻译之SHUFFLE 6月24日
  8. mysql字符集排序规则_Mysql 字符集及排序规则
  9. linux 问题一 apt-get install 被 lock
  10. 使用.net Stopwatch class 来分析你的代码
  11. SQLite允许向一个integer型字段中插入字符串
  12. 基于JAVA+SpringMVC+Mybatis+MYSQL的宠物商城
  13. C语言:进制转换(整数、字符串)
  14. c++builder ping_C++实现ping功能转
  15. 【懒懒】我不生产笑话,我只是笑话的搬运工 [问题点数:200分]
  16. caffe框架学习(layer)
  17. 网吧里电脑提示计算机内存不足,网吧电脑显示虚拟内存不足该怎么办呢
  18. Python升级pip失败解决办法
  19. 程序员免费自学编程的8大网站!
  20. 软连接ln -s 创建以及删除

热门文章

  1. 【Lucene4.8教程之中的一个】使用Lucene4.8进行索引及搜索的基本操作
  2. centos7 docker安装和使用_入门教程
  3. HDU 1727 Hastiness(模拟)
  4. 可爱的 Python: 使用 mechanize 和 Beautiful Soup 轻松收集 Web 数据
  5. java中的getfirst_Java LinkedList getFirst()用法及代码示例
  6. 全国计算机等级考试题库二级C操作题100套(第93套)
  7. 下列属于计算机人工智能应用领域的是多选题,每天五道选择题(10)
  8. arm-2014.05 编译三星内核错误 “not support ARM mode ‘smc 0’ ”
  9. java获取response数据_Java中实现Http请求并获取响应数据
  10. java resourcebundle_Java - Properties和ResourceBundle类学习