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.


【题目分析】

相比较上一个题目,在这个问题的grid中有部分区域是存在障碍物的,即有些区域不能到达。


【思路】

我们分析一下如果存在障碍物这个问题我们该如何解决。

1. 如果障碍物在入口或者出口,那么总的方法数就是0;

2. 如果障碍物在其他位置,那么存在障碍物的位置能到达的方法数为零;

在上一题中用到的方法在这个题目中也适用,只是需要判定一下当前是否是障碍物,如果是障碍物,则直接把能到达当前位置的方法数设置为0,否则:A[j] = A[j] +  A[j - 1];

如上图中,黑框表示该位置存在障碍物,那么能到达这个位置的方法数只能被设置为0,然后按照动态规划的方法一行一行遍历,求出能到达每一个位置的方法数,直到求出能到达出口的方法数为止。


【java代码】

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

LeetCode OJ 63. Unique Paths II的更多相关文章

  1. 【一天一道LeetCode】#63. Unique Paths II

    一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...

  2. 【LeetCode】63. Unique Paths II

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

  3. 【LeetCode】63. Unique Paths II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  4. [leetcode DP]63. Unique Paths II

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

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

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

  6. [LeedCode OJ]#63 Unique Paths II

     [ 声明:版权全部,转载请标明出处,请勿用于商业用途.  联系信箱:libin493073668@sina.com] 题目链接:https://leetcode.com/problems/uniqu ...

  7. leetcode@ [62/63] Unique Paths II

    class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleG ...

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

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

  9. 62. Unique Paths && 63 Unique Paths II

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

随机推荐

  1. sort vector - leetcode 新用法

    179. Largest Number sort(num.begin(), num.end(), [](int a, int b){ return to_string(a)+to_string(b) ...

  2. 今天学习的裸板驱动之存储控制器心得(初始化SDRAM)

    CPU只管操作地址,而有些地址代表的是某些存储设备. 但是操作这些存储设备需要很多东西,比如需要制定bank,行/列地址等.所以就有了存储管理器,用来处理这种CPU操作的地址和存储设备间的转换. (1 ...

  3. Unity3D脚本使用:游戏对象访问

    Unity3D中用到的组件 组件在js中对应的对象 使用如图: 注意:一个物体可以添加多个组件和多个js 同个物体上添加的js间引用

  4. Linux软件安装管理 - CentOS

    ---恢复内容开始--- 1. 软件包管理简介 1.1 源码包 - 脚本安装包 1.2 二进制包(RPM包,系统默认包) - 依赖性 2. rpm命令管理(Redhat Package Manager ...

  5. 1、API

    基本API sectionsColor:['green','orange','red','grey'],//为每一层设置背景颜色 controlArrows:true,//是否显示幻灯片的左右按钮 v ...

  6. 编译搭建Lamp服务器

    Lamp 是目前倍受欢迎的一种网站服务器.其主要有linux+apache+mysql+php 组成.由于其组成成员都是开源免费的产品,所以被作为中小型网站服务器的选择.LZ之前在学校学linux的时 ...

  7. ios开发判断手机是否安装微信app

    1.代码如下 if ([WXApi isWXAppInstalled]) 2.如果以上代码无效,请在plist文件中添加如下内容

  8. ASP.NET使用ImageMap控件

    文章来自:http://www.baike369.com/content/?id=5773

  9. Gulp安装使用教程

    题记:为什么要使用gulp,网上有很多关于gulp的优势,而在我看来,这些都是工具的优势!工具的优势最主要体现在易用性上,听说gulp比grunt更易用,所以这里写个文档记录. 同样要保证nodejs ...

  10. ubuntu开放防火墙端口

    root@jbxue:$ sudo ufw enable  Firewall started and enabled on system startup  root@jbxue:$ sudo ufw ...