题目:

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. 全民wifi钓鱼来临----agnes安卓wifi钓鱼神器介绍

    断断续续搞了一些无线的东西,从bt5的aircrack-ng的破无线(没怎么成功过)其实EWSA这个用GPU跑还算不错,可惜了我这显卡也只能每秒2500,到用c118在OsmocomBB基础上进行gs ...

  2. WPF自定义控件(二)——TextBox

    和之前一样,先来看看效果: 这个TextBox可设置水印,可设置必填和正则表达式验证. 验证?没错,就是验证! 就是在输入完成后,控件一旦失去焦点就会自动验证!会根据我开放出来的“是否可以为空”属性进 ...

  3. [转]bat中的特殊字符,以及需要在bat中当做字符如何处理

    bat中的特殊字符,以及需要在bat中当做字符如何处理 批处理.Bat 中特殊符号的实际作用,Windows 批处理中特殊符号的作用: @ \\隐藏命令的回显. ~ \\在for中表示使用增强的变量扩 ...

  4. JAVA面试题集之基础知识

                           JAVA面试题集之基础知识 基础知识:  1.C 或Java中的异常处理机制的简单原理和应用. 当JAVA程序违反了JAVA的语义规则时,JAVA虚拟机就 ...

  5. MYSQL-给带特殊符号的数据库创建用户名

    MYSQL-创建数据库及用户名: mysql> create database yoon;Query OK, 1 row affected (0.00 sec) mysql> grant ...

  6. 进程和cpu绑定

    #include<stdlib.h> #include<stdio.h> #include<sys/types.h> #include<sys/sysinfo ...

  7. 【js】 流式布局 页面

    <!DOCTYPE html><html><head> <meta content="text/html; charset=utf-8" ...

  8. python学习小结7:变量类型

    变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据类型,这些变量可以存储整 ...

  9. IOS开发实现录音功能

    导入框架: ? 1 #import <AVFoundation/AVFoundation.h> 声明全局变量: ? 1 2 3 4 5 @interface ViewController ...

  10. 面试问到的Spring

    一.介绍Spring 1.主要使用了基本的javabean代替的Ejb  Ejb:服务端的组件模型,设计目标应用部署分布在应用程序,把已经做好的编好的程序,打包放在服务 端执行,凭借java跨平台的优 ...