题目

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 Path是一样的。

只是要单独考虑下障碍物对整个棋盘的影响。

先看看初始条件会不会受到障碍物的影响。

假设整个棋盘只有一行,那么在第i个位置上设置一个障碍物后,说明位置i到最后一个格子这些路都没法走了。

如果整个棋盘只有一列,那么第i位置上的障碍物,也会影响从第i位置往后的路。

所以说明,在初始条件时,如果一旦遇到障碍物,障碍物后面所有格子的走法都是0。

再看求解过程,当然按照上一题的分析dp[i][j] = dp[i-1][j] + dp[i][j-1] 的递推式依然成立(机器人只能向下或者向右走嘛)。但是,一旦碰到了障碍物,那么这时的到这里的走法应该设为0,因为机器人只能向下走或者向右走,所以到这个点就无法通过。

处理完障碍物的特殊问题,依照unique paths改一下代码就好。

代码如下:

 1    public int uniquePathsWithObstacles(int[][] obstacleGrid) {  
 2         int m = obstacleGrid.length;  
 3         int n = obstacleGrid[0].length;  
 4         
 5         if(m==0||n == 0)  
 6             return 0; 
 7         
 8         if(obstacleGrid[0][0] == 1 || obstacleGrid[m-1][n-1] == 1)  
 9             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];  
     } 

Unique Paths II leetcode java的更多相关文章

  1. Unique Paths II [LeetCode]

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

  2. Unique Paths II ——LeetCode

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

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

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

  4. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  5. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

  6. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  7. [Leetcode Week12]Unique Paths II

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

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

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

  9. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

随机推荐

  1. KVM源代码阅读--内核版本3.17.4

    为了更加深入的学习虚拟化,因此我必须把KVM源代码搞清楚,这是一个必须要挖的坑.我会把自己的一些阅读的代码贴上来,可能会有理解不对的地方,希望和大家一起交流,请多提意见,以便于纠正错误.所用的内核版本 ...

  2. 李善友《认知升级之第一性原理》--507张PPT全解!_搜狐科技_搜狐网

      http://www.sohu.com/a/151470602_733114

  3. C#——性能计数器

    简要Windows性能监视器: 打开Windows性能监视器的步骤如下: 开始→运行→perfmon→确定 在这里我们可以选择添加我们要监控的计数器,比如:cpu使用率.内存使用量等,作为asp.ne ...

  4. STM32的PWM输入模式设置并用DMA接收数据

    参考 :STM32输入捕获模式设置并用DMA接收数据 PWM input mode This mode is a particular case of input capture mode. The ...

  5. STM32F4: Generating parallel signals with the FSMC

    STM32F4: Generating parallel signals with the FSMC The goal: The memory controller can be used to ge ...

  6. Android记录3--ExpandableListView使用+获取SIM卡状态信息

    Android记录3--ExpandableListView使用+获取SIM卡状态信息 2013年8月9日Android记录 ExpandableListView是一个可以实现下拉列表的控件,大家可能 ...

  7. ADO.NET理论+实践

    题记: 每一事物的产生和存在都有其特定的理由.  理论:ADO.NET是一组与数据源进行交互的面向对象类库.通常情况下数据源就是数据库,当然同样也能是文本文件,Excel表格或XML文件,我们知道的数 ...

  8. WebLogic使用总结(二)——WebLogic卸载

    一.WebLogic 12c的卸载 WebLogic的卸载是非常容易的,找到WebLogic的卸载程序,如下图所示: 启动卸载程序,如下图所示:

  9. C#获取文件夹及文件的大小与占用空间的方法

    本文详细介绍了利用C#实现根据路径,计算这个路径所占用的磁盘空间的方法 . 网上有很多资料都是获取文件夹/文件的大小的.对于占用空间的很少有完整的代码.这里介绍实现这一功能的完整代码,供大家参考一下. ...

  10. 加快Qemu Aarch32虚拟开发板的启动速度

    软件版本 Qemu: 2.8.0 虚拟开发板: vexpress-ca9 概述 之前的博文介绍了将Python移植到开发板上, 根文件系统采用的是ramdisk, 这个文件系统的缺点是修改的内容重启会 ...