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 ...
随机推荐
- 用python实现哈希表
哈哈,这是我第一篇博客园的博客.尝试了一下用python实现的哈希表,首先处理冲突的方法是开放地址法,冲突表达式为Hi=(H(key)+1)mod m,m为表长. #! /usr/bin/env py ...
- C#获取数据库中的Instance
如果我现在要写个代码生成器,连接数据库,那你得知道有哪些Database存在吧,不然咋整? 在VS中我们添加一个ADO.NET的实体模型 在选择数据库名称的时候就是获取了数据库中Database In ...
- Oracle RAC中的一台机器重启以后无法接入集群
前天有个同事说有套AIX RAC的其中一台服务器重启了操作系统以后,集群资源CSSD的资源一直都在START的状态,检查日志输出有如下内容: [ CSSD][1286]clssnmv ...
- Ubuntu 14.04 安装 Xilinx ISE 14.7 全过程
生命在于折腾. 这个帖子作为我安装xilinx ISE 14.7版本一个记录.希望给需要的人一些帮助,这些内容绝大部分也是来源于互联网. 软硬件: lsb_release -a No LSB modu ...
- java并发编程(一)
多个线程访问同一个变量时,可能会出现问题.这里我用两个线程同时访问一个int count变量,让他们同时+1.同时让线程睡眠1秒,每个线程执行10次,最后应该输出20才对,因为count++并不是原子 ...
- Web开发从零单排之二:在自制电子请帖中添加留言板功能,SAE+PHP+MySql
在上一篇博客中介绍怎样在SAE平台搭建一个html5的电子请帖网站,收到很多反馈,也有很多人送上婚礼的祝福,十分感谢! web开发从零学起,记录自己学习过程,各种前端大神们可以绕道不要围观啦 大婚将至 ...
- JAVASCRIPT、ANDROID、C#分别实现普通日期转换多少小时前、多少分钟前、多少秒
貌似最近很流行这个,就写了个js函数实现之 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ...
- IOS 后台运行
默认情况下,当app被按home键退出后,app仅有最多5秒钟的时候做一些保存或清理资源的工作.但是应用可以调用UIApplication的beginBackgroundTaskWithExpirat ...
- eclipse中加放js文件报js语法错误解决办法
1) eclipse设置 window->preference-> JavaScript -> Validator->Errors/Warnings->E ...
- 初识PCA数据降维
PCA要做的事降噪和去冗余,其本质就是对角化协方差矩阵. 一.预备知识 1.1 协方差分析 对于一般的分布,直接代入E(X)之类的就可以计算出来了,但真给你一个具体数值的分布,要计算协方差矩阵,根据这 ...