这是“不同路径” 的进阶问题:
现在考虑网格中有障碍物。那样将会有多少条不同的路径从左上角到右下角?
网格中的障碍物和空位置分别用 1 和 0 来表示。
例如,
如下所示在 3x3 的网格中有一个障碍物。
[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]
一共有 2 条不同的路径从左上角到右下角。
注意: m 和 n 的值均不超过 100。
详见:https://leetcode.com/problems/unique-paths-ii/description/

Java实现:

参考:https://www.cnblogs.com/springfor/p/3886644.html

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

参考:https://www.cnblogs.com/grandyang/p/4353680.html

063 Unique Paths II 不同路径 II的更多相关文章

  1. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  2. Java for LeetCode 063 Unique Paths II

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

  3. 【LeetCode】063. Unique Paths II

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

  4. [LeetCode] Unique Paths 不同的路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  5. [LeetCode] 62. Unique Paths 不同的路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  6. 【LeetCode每天一题】Unique Paths(唯一的路径数)

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The ...

  7. LeetCode OJ:Unique Paths(唯一路径)

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  8. LeetCode(63):不同路径 II

    Medium! 题目描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“F ...

  9. Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

    Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...

随机推荐

  1. Workerman安装流程

    第一步检测安装环境 curl -Ss http://www.workerman.net/check.php | php 操作结果显示 报错了  需要找到php.ini文件 解决办法如下: 打开 php ...

  2. spring属性注入DI

    spring setter方式注入: 注入对象属性: 前提: 在bean对应实体中有对应的setter方法. 基础代码: 在bean中有另一个bean属性的setter方法. package cn.i ...

  3. linux 多线程编程-读写者问题

    #include <cstdio> #include <pthread.h> #include <unistd.h> ]; int i,j; pthread_rwl ...

  4. hadoop编码问题,mapreduce中Tex与string的转化 乱码问题

    引用:http://blog.csdn.net/zklth/article/details/11829563 Hadoop处理GBK文本时,发现输出出现了乱码,原来HADOOP在涉及编码时都是写死的U ...

  5. 监控mysql主从同步状态是否异常

    监控mysql主从同步状态是否异常,如果异常,则发生短信或邮寄给管理员 标签:监控mysql主从同步状态是否异常 阶段1:开发一个守护进程脚本每30秒实现检测一次. 阶段2:如果同步出现如下错误号(1 ...

  6. 启用数据库 aspnetstate 会话状态

    http://www.cnblogs.com/klzwj1988/archive/2010/05/10/1731723.html

  7. JQuery中查找父元素,子元素,追加元素,插入元素和删除元素 及其他常用方法

    Jquery之所以强大,和其在获取对象时使用与css选择器兼容的语法有很大关系.而且它还兼容了CSS3的选择器,而且多出了不少. 所以jQuery的选择器也就变得很多很强大.就最基本的有以下四个: $ ...

  8. opengl1

    OpenGL Programming Guide Programming Guide > Chapter 1 Chapter 1 Introduction to OpenGL Chapter O ...

  9. # program once 用途 及与 ifndef使用异同

    在头文件中用这种写法就是为了该头文件被重复包含时不会出现符合重定义的错误. 效果等同于     #ifndef __xxx__     #define __xxx__     ...    #endi ...

  10. Newtonsoft.Json序列化字符串-格式化和时间格式问题

    最近C#中需要将实体进行json序列化,使用了Newtonsoft.Json public static void TestJson()        {            DataTable d ...