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.

 class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int rows = obstacleGrid.length;
int cols = obstacleGrid[0].length;
int[][] dp = new int[rows][cols]; for(int i = 0; i < rows;i++){
for (int j = 0; j < cols ;j++){
if(obstacleGrid[i][j]==1)
dp[i][j] = 0;
else{
if (i==0&& j==0)
dp[i][j] = 1;
else if (i==0)
dp[i][j] = dp[i][j-1]; //边界 else if (j == 0)
dp[i][j] = dp[i-1][j];
else
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
}
return dp[rows-1][cols-1];
}
}
 class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int rows = obstacleGrid.length;
int cols = obstacleGrid[0].length;
for(int i = 0; i < rows;i++){
for (int j = 0; j < cols ;j++){
if(obstacleGrid[i][j]==1)
obstacleGrid[i][j] = 0;
else if (i==0&& j==0)
obstacleGrid[i][j] = 1;
else if (i==0)
obstacleGrid[i][j] = obstacleGrid[i][j-1]*1; //边界,没有路径了,要么是0,要么是1 else if (j == 0)
obstacleGrid[i][j] = obstacleGrid[i-1][j]*1;
else
obstacleGrid[i][j] = obstacleGrid[i-1][j] + obstacleGrid[i][j-1];
}
}
return obstacleGrid[rows-1][cols-1];
}
}

63. Unique Paths II(有障碍的路径 动态规划)的更多相关文章

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

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

  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

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

  4. 62. Unique Paths && 63 Unique Paths II

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

  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. 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). ...

  7. leetcode 63. Unique Paths II

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

  8. 63. Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  9. 63. Unique Paths II(中等, 能独立做出来的DP类第二个题^^)

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

随机推荐

  1. django 模型中的计算字段

    models.py class Person(models.Model): family_name= models.CharField(max_length=20, verbose_name='姓') ...

  2. 杂记之--苹果4s手机呼叫转移怎么设置

    您好,呼叫转移只需在拨号界面输入相应的代码就可以,无需其他设置无条件转移 **21*电话号码#再按拨号键 取消代码:##21# *#21# 再按拨号键无信号,关机转移 **62*电话号码#再按拨号键 ...

  3. IOS6.0调用通讯录和之前的差别

    6.通讯录列表获取差异 自iOS6.0后获取通讯录列表需要询问用户,经过用户同意后才可以获取通讯录用户列表.而且ABAddressBookRef的初始化工作也由ABAddressBookCreate函 ...

  4. 三分搜索-ZOJ LightBulb

    开始算法基础学习的第一天 今天学习的内容是三分搜索 相对来说很基础的内容(还是觉得脑子不够用) 三分搜索主要用于凸函数查找极大值. (盗个图) 如图所示 若要查找该函数的最大值 可以考虑和二分法一样的 ...

  5. oracle如何用sql查看触发器?

    ORACLE查出表所有的触发器及触发器详细信息 一.查all_triggers表得到trigger_name Sql代码 select trigger_name from all_triggers w ...

  6. PHP疑难杂症

    下面这种写法是否允许? echo '\n' // \n echo "\n" // 输出换行 直接访问对象不存在的属性,会怎样? $o = new stdClass(); echo ...

  7. 170308、oracle查看被锁的表和解锁

    --以下几个为相关表SELECT * FROM v$lock;SELECT * FROM v$sqlarea;SELECT * FROM v$session;SELECT * FROM v$proce ...

  8. java发展历程、常用dos命令与jDK工具使用

    Java菜鸟学习之旅 1.勤敲代码 2.必须将课堂上讲的内容学到位 2.1 学到会用 2.2 在学会之前不要自作主张 2.3 当天课程当天完成 3.能说会道 3.1 善于表达 3.2 学会的都能讲 4 ...

  9. Spoken English Practice(And I can't walk. Should i just stay home and pout about it?)

    绿色:连读:                  红色:略读:               蓝色:浊化:               橙色:弱读     下划线_为浊化 口语蜕变(2017/7/12) ...

  10. 【Python算法】图与树的实现

    邻接列表及其类似结构 对于图结构的实现来说,最直观的方式之一就是使用邻接列表.下面我们来实现一个最简单的:假设现在我们有n个节点,编号分别为0,...,n-1. 然后,每个邻接列表就是一个数字列表,我 ...