这是“不同路径” 的进阶问题:
现在考虑网格中有障碍物。那样将会有多少条不同的路径从左上角到右下角?
网格中的障碍物和空位置分别用 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. win7 jenkins 修改主目录

    1.安装tomcat 2.下载Jenkins.war包,把Jenkins.war放在D:\01Install\tomcat\webapps目录下,启动tomcat

  2. Hadoop安装全教程 Ubuntu14.04+Java1.8.0+Hadoop2.7.6

    最近听了一个关于大数据的大牛的经验分享,在分享的最后大牛给我们一个他之前写好的关于大数据和地理应用demo.这个demo需要在Linux环境上搭建Hadoop平台.这次就简单的分享一下我关于在 Lin ...

  3. python 抓取美丽说店铺的宝贝图片及详细信息的实现(爬虫)

    对于页面的抓取,我们使用的是requests,现在大部分的网站都支持动态加载,我们在firefox f12后查找动态的url :http://www.meilishuo.com/aj/shop_lis ...

  4. MangoDB篇章(1)

    关系型数据库遵循ACID规则 事务(transaction)4个特性:原子性(A).一致性(C).独立性(I).持久性(D) : 分布式系统(distributed system): 由多台计算机和通 ...

  5. CentOS虚拟机通过主机WIFI上网

    0 环境简介 环境如下: (1)宿主机为WIN7系统,连接内网,同时通过本机的WIFI上外网: (2)虚拟机为VMWare12下的CentOS7系统. 1 虚拟机设置 选择NAT 模式: 2 宿主机W ...

  6. 《Java多线程编程核心技术》读后感(十六)

    线程组 线程组的作用是,可以批量的管理线程或线程组对象,有效地对线程或线程组对象进行组织 线程对象关联线程组:1级关联 package Seven; public class ThreadA exte ...

  7. Javascript中的"\r\n"

    我们知道 \r 代表的是 回车符(ACSII: 13 或0x0d), 也就是"硬回车" \n 代表的是 换行符(ACSII: 10 或 0x0a), 也就是 "软回车&q ...

  8. HDU - 1535 Invitation Cards 前向星SPFA

    Invitation Cards In the age of television, not many people attend theater performances. Antique Come ...

  9. ASP.NET in C#,ClientScript.RegisterStartupScript与ClientScript.RegisterClientScriptBlock用法之己见

    ClientScript.RegisterStartupScript:http://msdn.microsoft.com/zh-cn/library/system.web.ui.clientscrip ...

  10. PHP正则表达式,看这一篇就够啦!

    前言 不知道你们有没有这个感觉,看正则表达式就像看天文数字一样,什么电话号码.邮箱的正则表达式,上网复制一下粘贴下来就搞定了.完全不知道这写的是什么玩意.后来我自己也想学一下,因为感觉用处还是挺大的. ...