题目:

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.

Hide Tags

Array Dynamic Programming

 

链接:  http://leetcode.com/problems/unique-paths-ii/

题解:

也是DP问题,Unique Path一样可以in place解决。要点是在设置第一行和第一列碰到obstacle的时候,要将其以及之后的所有值设置为零,因为没有路径可以达到。之后在DP扫描矩阵的时候,也要讲obstacle所在的位置清零。

Time Complexity - O(m * n), Space Complexity - O(1)。

public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int rowNum = obstacleGrid.length, colNum = obstacleGrid[0].length;
if(obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0][0] == 1){
return 0;
} for(int row = 0; row < rowNum; row ++){
if(obstacleGrid[row][0] == 0)
obstacleGrid[row][0] = 1;
else if (obstacleGrid[row][0] == 1){ //if find obstacle, set all [row,0] below obstacle to 0
for(int tempRow = row; tempRow < rowNum; tempRow ++)
obstacleGrid[tempRow][0] = 0;
break;
}
} for(int col = 1; col < colNum; col ++){
if(obstacleGrid[0][col] == 0)
obstacleGrid[0][col] = 1;
else if (obstacleGrid[0][col] == 1){ // //if find obstacle, set all [0,col] one the right of obstacle to 0
for(int tempCol = col; tempCol < colNum; tempCol ++)
obstacleGrid[0][tempCol] = 0;
break;
}
} for(int i = 1; i < rowNum; i ++){
for(int j = 1; j < colNum; j ++){
if(obstacleGrid[i][j] == 1)
obstacleGrid[i][j] = 0;
else
obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1];
}
} return obstacleGrid[rowNum - 1][colNum - 1];
}
}

二刷:

和一刷一样, 就是先判断行和列中的obstacle元素,将其与其之后的为止置零。接下来遍历整个矩阵。

Java:

Time Complexity - O(m * n), Space Complexity - O(1)。

public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if (obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0][0] == 1) {
return 0;
}
int rowNum = obstacleGrid.length, colNum = obstacleGrid[0].length;
for (int i = 0; i < rowNum; i++) {
if (obstacleGrid[i][0] == 1) {
for (int k = i; k < rowNum; k++) {
obstacleGrid[k][0] = 0;
}
break;
} else {
obstacleGrid[i][0] = 1;
}
}
for (int j = 1; j < colNum; j++) {
if (obstacleGrid[0][j] == 1) {
for (int k = j; k < colNum; k++) {
obstacleGrid[0][k] = 0;
}
break;
} else {
obstacleGrid[0][j] = 1;
}
}
for (int i = 1; i < rowNum; i++) {
for (int j = 1; j < colNum; j++) {
if (obstacleGrid[i][j] == 1) {
obstacleGrid[i][j] = 0;
} else {
obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1];
}
}
}
return obstacleGrid[rowNum - 1][colNum - 1];
}
}

三刷:

Java:

public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if (obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0][0] == 1) {
return 0;
}
int rowNum = obstacleGrid.length, colNum = obstacleGrid[0].length;
for (int i = 0; i < rowNum; i++) {
if (obstacleGrid[i][0] == 1) {
for (int k = i; k < rowNum; k++) {
obstacleGrid[k][0] = 0;
}
break;
} else {
obstacleGrid[i][0] = 1;
}
}
for (int j = 1; j < colNum; j++) {
if (obstacleGrid[0][j] == 1) {
for (int k = j; k < colNum; k++) {
obstacleGrid[0][k] = 0;
}
break;
} else {
obstacleGrid[0][j] = 1;
}
}
for (int i = 1; i < rowNum; i++) {
for (int j = 1; j < colNum; j++) {
obstacleGrid[i][j] = obstacleGrid[i][j] == 1 ? 0 : obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1];
}
}
return obstacleGrid[rowNum - 1][colNum - 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. 62. Unique Paths && 63 Unique Paths II

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

  3. 【LeetCode】63. Unique Paths II

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

  4. [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 ...

  5. leetcode 63. Unique Paths II

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

  6. LeetCode OJ 63. Unique Paths II

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

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

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

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

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

  9. [leetcode DP]63. Unique Paths II

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

随机推荐

  1. DOS命令之----Netstat+Task以及相关使用

    作为一个初步接触电脑的人,在学习Android的过程中,遇到各种问题,今天遇到了.这样一个错误提示: The connection to adb is down, and a severe error ...

  2. MVC 5.0(or5.0↓) Ajax.BeginForm 异步上传附件问题,答案是不能的!

    MVC 5.0(or5.0↓)  Ajax.BeginForm 异步上传附件问题,答案是不能的! (请注意我这里说的异步!) 来看一下下面这段一步提交file的代码 //前台 .cshtml 文件 & ...

  3. Tools for Presention

    ZoomIt v4.5 http://technet.microsoft.com/en-us/sysinternals/bb897434.aspx 微软的教师演示工具 主要有放大,画图,倒计时的功能. ...

  4. mysql 连接语句

    在 SELECT 语句中,如果 FROM 子句引用了多个表源或视图,可以使用 JOIN 指示指定的联接操作应在指定的表源或视图之间执行. 一.交叉联接:CROSS JOIN 交叉联接将执行一个叉积(迪 ...

  5. C预处理,条件编译

    预处理是指在编译器之前运行,常以“#”开头 包含3个方面的内容: 1)宏定义与宏替换 2)文件包含 3)条件编译 宏定义与宏替换: 宏名一般大写,替换发生在编译之前,且是机械替换,不做语法检查,也不分 ...

  6. cocos2dx中的假动作,又称动作回调函数

    1.动作与动画的区别 动作是:定时器+属性的改变,是帧循环的累积效应 动画是:帧图片的播放效果,我们知道电影的播放就是快速播放的胶片,这就是动画的原理 2.假动作:又称动作回调函数 四大类假动作: c ...

  7. Week1 Team Homework #3: 软件工程在北航

    在组内成员的共同努力,我们采访了几个学长学姐,顺利完成任务.反馈信息如下: 平均每周花在这门课上的时间 平均写的代码总行数 学到的最有用的部分 最没用的部分 <软件工程>最应该改进的地方 ...

  8. iOS中 @synthesize 和 @dynamic

    今天写点过时的东西,我记得  这个是xcode 4 那个年代的事情了,没想到有时候大家还会被问到.可能目的就是看看你是从是么时候才开始接触iOS的. 在声明property属性后,有2种实现选择 @s ...

  9. wap开发使用jquery mobile之后页面不加载外部css样式文件/js文件

    场景: wap开发,使用jquery mobile之后不会加载外部自定义的css文件了,需要手动刷新才会加载,查看外部自定义的js文件也是一样. 解决办法: 1.在page下面添加css样式,就不要写 ...

  10. WPFMediaKit照相功能

    最近写的一个WPF照相功能,往各位吐槽,提供优化 在WPF 设计器中添加如下代码 xmlns:wpfmedia="clr-namespace:WPFMediaKit.DirectShow.C ...