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

    .#!/usr/bin/perl -w 内建警告信息,Perl发出警告 .字符串 . 连接符 .重复次数 .字符串与数字之间的自动转换 .; + += *= .= not and or xor .pr ...

  2. PS 滤镜— —图像偏移

    clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...

  3. bzoj 1202 [HNOI2005]狡猾的商人——带偏移量的并查集

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1202 带偏移量的并查集. 注意先 find() 再调用 dis !!! 自己的对拍太水了. ...

  4. 非常好的LINUX学习者博客

    http://blog.csdn.net/qq_21794823/article/category/6496200

  5. SpringMVC之六:Controller详细介绍

    一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Mo ...

  6. linux 遍历目录+文件(优化版本)

    c++17 filesystem, regex 遍历目录 #include <stdio.h> #include <sys/types.h> #include <dire ...

  7. 【opencv学习笔记七】访问图像中的像素与图像亮度对比度调整

    今天我们来看一下如何访问图像的像素,以及如何改变图像的亮度与对比度. 在之前我们先来看一下图像矩阵数据的排列方式.我们以一个简单的矩阵来说明: 对单通道图像排列如下: 对于双通道图像排列如下: 那么对 ...

  8. (六)编写基类BaseDao

    在action中继承了ActionSupport和其它一些公共属性,如selectedRow等:可能以后还会产生更多公共的内容,所以应该把这些共有的抽取出来,放入到一个基本action中,我们命名为B ...

  9. centos6.5 下安装 sqlplus

    1.下载下面的 rpm 文件 oracle-instantclient12.-basic--.x86_64.rpm oracle-instantclient12.-devel--.x86_64.rpm ...

  10. Object有哪些公用的方法?

    Object是所有类的父类,任何类都默认继承Object. public class Demo { public static void main(String[] args) { Object ob ...