题目:

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. easyui菜单栏的使用

    <div id="tabs" class="easyui-tabs" data-options="plain:true,fit:true,bor ...

  2. 过拟合(Overfitting)和正规化(Regularization)

    过拟合: Overfitting就是指Ein(在训练集上的错误率)变小,Eout(在整个数据集上的错误率)变大的过程 Underfitting是指Ein和Eout都变大的过程 从上边这个图中,虚线的左 ...

  3. C string 函数大全

    PS:本文包含了大部分strings函数的说明,并附带举例说明.本来想自己整理一下的,发现已经有前辈整理过了,就转了过来.修改了原文一些源码的问题,主要是用char *字义字符串的问题,导致程序运行时 ...

  4. Hive内表和外表的区别

    本文以例子的形式介绍一下Hive内表和外表的区别.例子共有4个:不带分区的内表.带分区的内表.不带分区的外表.带分区的外表. 1 不带分区的内表 #创建表 create table innerTabl ...

  5. 短小强悍的JavaScript异步调用库

    对于博文 20行完成一个JavaScript模板引擎 的备受好评我感到很惊讶,并决定用此文章介绍使用我经常使用的另一个小巧实用的工具.我们知道,在浏览器中的 JavaScript 绝大部分的操作都是异 ...

  6. 使用go语言后的感受

    前两天我说过为了学习go语言去学习了一遍python,当我完成了python的学习后,昨天中午就去学习了go语言.以下简称之为golang. 我用的操作系统是windows xp,golang对xp还 ...

  7. ASP.NET Web – 状态管理

    状态类型 客户端或服务器资源 有效时间 ViewState 客户端 只在一个页面中 Cookie 客户端 关闭浏览器时会删除临时cookie,永久cookie存储在客户系统的磁盘上 Session 服 ...

  8. Linux下卸载和安装MySQL[rpm包]

    一.卸载原来的mysql: 卸载一: 输入: #rpm -qa | grep -i mysql 显示: mysql-libs-5.1.52-1.el6_0.1.i686 卸载方法: yum -y re ...

  9. Flv 视频格式(转)

    最近要用到flv,整理了一些flv格式的资料,供参考. flv文件主要由两部分组成:header和body. 1.header header部分记录了flv的类型.版本等信息,是flv的开头,一般都差 ...

  10. 【BZOJ】【2561】最小生成树

    网络流/最小割 对于所有小于L的边求一个割使得U,V不连通,这样就可以保证L可能在最小生成树里. 最大生成树同理. 答案累加一下即可.(Orz Hzwer) (我一开始怎么会sb地去想到一起求呢……) ...