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.

分析:

题中给出了m×n的方格,m和n最大值为100,左上角的小机器人只能每次向右走一步或者向下走一步,终点在方格的右下角,

求一共有多少条不同的路径。

最优的子结构: a[i,j] = a[i-1,j] + a[i,j-1]

第一行中,不管到达哪个点,都是沿着第一行一直走才能达到的,如果第一行的值为0,第一行的点的路径为1, a[i][0] = 1;

如果遇到了障碍物,为1的后面的方格都走不到了,所以跳出循环。

第一列中,不管到达哪个点,都是沿着第一列一直走才能达到的,如果方格内的值为0,第一行的点的路径为1, a[0][j] = 1;

如果遇到了障碍物,为1的后面的方格都走不到了,所以跳出循环。

从起点到方格内某个点的不同路径数量可以分解为该点上方的点和左侧的点路径之和。

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

leetcode 63. Unique Paths 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. [LeetCode] 63. Unique Paths II 不同的路径之二

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

  3. LeetCode: 63. Unique Paths II(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/

  4. [leetcode] 63. Unique Paths II (medium)

    原题 思路: 用到dp的思想,到row,col点路径数量 : path[row][col]=path[row][col-1]+path[row-1][col]; 遍历row*col,如果map[row ...

  5. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  6. 【LeetCode】63. Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

  7. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  8. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

  9. 【leetcode】Unique Paths II

    Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...

随机推荐

  1. IOS VFL屏幕自适应

    -(void)fun1{ //注意使用VFL,不用设置视图的frame UIView *view = [[UIView alloc] init]; view.backgroundColor = [UI ...

  2. git 推送

    echo "# shops" >> README.md git init git add README.md git commit -m "first com ...

  3. Win7环境下Eclipse连接Hadoop2.2.0

    准备: 确保hadoop2.2.0集群正常运行 1.eclipse中建立java工程,导入hadoop2.2.0相关jar包 2.在src根目录下拷入log4j.properties,通过log4j查 ...

  4. 解决问题--VS2012中一个Panel覆盖另一个Panel时拖动时容易造成两个控件成父子关系的避免

    在*.Designer.cs中,假如想把panel1覆盖到panel2上,但是VS自动让panel1成为panel2的子控件了,在文件中会有this.panel2.Controls.Add(this. ...

  5. sublime中安装css 格式化插件

    HTML-CSS-JS Prettify解决问题 今天发现此插件依赖于nodejs,如果本机没有安装nodejs,会一直提示你安装,解决方法很简单,直接brew install nodejs即可 看看 ...

  6. Drupal 7.31SQL注入getshell漏洞利用详解及EXP

    0x00 这个漏洞威力确实很大,而且Drupal用的也比较多,使用Fuzzing跑字典应该可以扫出很多漏洞主机,但是做批量可能会对对方网站造成很大的损失,所以我也就只是写个Exp不再深入下去. 0x0 ...

  7. APP抓链接工具(Fiddler版)

    1.下载Fiddler源代码: http://pan.baidu.com/s/1hqEUK0O 2.修改如下源代码: 3.运行截图:

  8. Python之路【第十一篇续】前端之CSS补充

    CSS续 1.标签选择器 为类型标签设置样式例如:<div>.<a>.等标签设置一个样式,代码如下: <style> /*标签选择器,如果启用标签选择器所有指定的标 ...

  9. Orchard源码分析(5.2):BeginRequest事件处理(DefaultOrchardHost.BeginRequest方法)

    BeginRequest事件处理的作用是确保所有Shell已经加载,或者在扩展有变化的时候重新加载.          void IOrchardHost .BeginRequest() {      ...

  10. winsow xp不能安装软件, 提示"中断" 是因为设置了 软件限制策略

    原来是我为了优化和安全, 设置了软件限制策略. 我设置的是: secpol.msc中, 设置 "软件限制策略" -> "其他规则"中 , 指定了 c:/d ...