63. Unique Paths II
题目:
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.
链接: 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的更多相关文章
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- [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 ...
- leetcode 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LeetCode OJ 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 63. Unique Paths II(中等, 能独立做出来的DP类第二个题^^)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【一天一道LeetCode】#63. Unique Paths II
一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...
- [leetcode DP]63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
随机推荐
- openerp模块收藏 基于Lodop的报表打印模块(转载)
基于Lodop的报表打印模块 原文:http://shine-it.net/index.php/topic,7397.0.html 前段时间写了个小模块,来解决OE中报表打印不方便的问题.借鉴了 @b ...
- apache配置VirtualHost(windows)
以下方式适合原生 Apache, XAMPP 和WAMP 套件. 1. 打开目录 {Apache2 安装目录}\conf\extra\, 找到 httpd-vhosts.conf 文件. 2. 仿照例 ...
- 转学步园:jquery offset
JQuery Offset实验与应用 我们有时候需要实现这样一种功能:点击一个按钮,然后在按钮的下方显示一个div.当按钮位于角落时,div的位置设定就需要计算,使div完全显示. 我打算使用offs ...
- HTTP 错误 404.3 - Not Found
在使用win2012服务器上的IIS发布网页的时候,出现下面的错误 解决办法: 将应用程序开发下的所有功能都安装. 如果上面的方法没解决问题的话,那么看看下图中的这些安装没,没有的话就继续安装.
- python之类定义
<python基础教程>第7章说python中的类定义: 1. 要么声明__metaclass__=type 2. 要么继承object. 但是直接定义下类, 也没报错: >> ...
- Async详解之一:流程控制
为了适应异步编程,减少回调的嵌套,我尝试了很多库.最终觉得还是async最靠谱. 地址:https://github.com/caolan/async Async的内容分为三部分: 流程控制:简化十种 ...
- C++中的运算符优先级
1 () [] . ->2 ! ~ -(负号) ++ -- &(取变量地址)* (type)(强制类型) sizeof 3 * / % 4 + ...
- 屏蔽ios7中某个页面的默认手势滑回返回
- (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:YES];self.navigationController.in ...
- Jquery datatables 重载数据方法
参考这里 { RefreshTable('#table-example', '/BlogManage/GetLabelData'); } function RefreshTable(tableId, ...
- 【最小生成树】BZOJ 1196: [HNOI2006]公路修建问题
1196: [HNOI2006]公路修建问题 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1435 Solved: 810[Submit][Sta ...