题目:

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

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.

Note: m and n will be at most 100.

Example 1:

Input:
[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]
Output: 2
Explanation:
There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
1. Right -> Right -> Down -> Down
2. Down -> Down -> Right -> Right

分析:

和第62题思路类似,LeetCode 62. Unique Paths不同路径 (C++/Java)

现在网格中有了障碍物,网格中的障碍物和空位置分别用1和 0来表示。

无论是递推还是递归求解,只要加一个判断当前各自是否有障碍即可。在判断当前是否有解时,可以在最开始将二维数组全部赋值-1,如果求到子问题时不为-1,则可以直接返回已有的解,可以节省时间。

注:1.起始位置可能有障碍物。

  2.还遇到一个问题: runtime error: signed integer overflow: 1053165744 + 1579748616 cannot be represented in type 'int' (solution.cpp),改成long即可。

程序:

C++

//Solution 1
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[].size();
vector<vector<long>> res(m+, vector<long>(n+, ));
for(int i = ; i < m+; ++i)
for(int j = ; j < n+; ++j){
if(obstacleGrid[i-][j-] == ){
res[i][j] = ;
}
else if(i == && j == ){
res[i][j] = ;
}
else{
res[i][j] = res[i-][j] + res[i][j-];
}
}
return res[m][n];
}
};
//Solution 2
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[].size();
res = vector<vector<int>>(m+, vector<int>(n+, -)); return solvePath(m, n, obstacleGrid);
}
private:
vector<vector<int>> res;
int solvePath(int m, int n, vector<vector<int>> &vec){
if(m <= || n <= ) return ;
if(m == && n == ) return -vec[][];
if(res[m][n] != -) return res[m][n];
if(vec[m-][n-] == ){
res[m][n] = ;
}
else{
res[m][n] = solvePath(m-, n, vec) + solvePath(m, n-, vec);
}
return res[m][n];
}
};

Java

class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
res = new int[m+1][n+1];
for(int[] r : res)
Arrays.fill(r,-1);
return solvePath(m, n, obstacleGrid);
}
private int[][] res;
private int solvePath(int m, int n, int[][] o){
if(m <= 0 || n <= 0) return 0;
if(m == 1 && n == 1) return 1-o[0][0];
if(res[m][n] != -1) return res[m][n];
if(o[m-1][n-1] == 1){
res[m][n] = 0;
}
else{
res[m][n] = solvePath(m-1, n, o) + solvePath(m, n-1, o);
}
return res[m][n];
}
}

LeetCode 63. Unique Paths II不同路径 II (C++/Java)的更多相关文章

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

  2. leetcode 63. Unique Paths II

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

  3. [LeetCode] 62. Unique Paths 不同的路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  4. [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming

    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(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/

  6. [leetcode] 63. Unique Paths II (medium)

    原题 思路: 用到dp的思想,到row,col点路径数量 : path[row][col]=path[row][col-1]+path[row-1][col]; 遍历row*col,如果map[row ...

  7. [LeetCode] 62. Unique Paths 唯一路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  8. &lt;LeetCode OJ&gt; 62. / 63. Unique Paths(I / II)

    62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: ...

  9. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

随机推荐

  1. npm --save-dev 和 --save 的区别

    转载 >>> 1. npm install 在安装 npm 包时,有两种命令参数可以把它们的信息写入 package.json 文件, 一个是npm install--save    ...

  2. 【Linux】文本处理工具介绍

    文本处理工具介绍 grep.sed和awk都是文本处理工具,各自都有各自的优缺点,一种文本处理命令是不能被另一个完全替换的.相比较而言,sed和awk功能更强大,且已独立成一种语言来介绍. grep: ...

  3. C语言--计算程序执行时间

    C语言–计算程序执行时间1. gettimeofday精度1us #include<stdio.h>#include<sys/time.h> int main(){ /* 定义 ...

  4. 学习UML类图

    在类图中一共包含以下几种模型元素,分别是:类(class).接口(interface)以及类之间的关系. 1.类(class) 在面向对象编程中,类是对现象世界中一组具有相同特征的物体的抽象. 2.接 ...

  5. windows 安装使用jupyter及 基础配置

    jupyter 是什么Jupyter Notebooks 是一个交互式笔记本,支持运行 40 多种编程语言,它的本质是一个 开源的 Web 应用程序,我们可以将其用于创建和共享代码与文档,他可以支持实 ...

  6. 用Java实现二叉查找树

    二叉查找树的实现 1. 原理 二叉查找树,又称为二叉排序树.二叉搜索树.对于树中每一个节点X,它的左子树中所有项的值小于X中的项,而它的右子树中所有项的值大于X中的项.二叉查找树的平均深度为O(log ...

  7. 新增SAP到OA接口,OA怎么更新WSDL给PI,怎么选择PI的IP地址(备忘)

    1.首先定义个class. 弄完以后可以使用http://IP地址:8088/seeyon/services/sapService?wsdl  进行导出,部署在哪个服务器就用哪个服务器的IP地址 pr ...

  8. Python笔记:设计模式之命令模式

    命令模式,正如模式的名字一样,该模式中的不同操作都可以当做不同的命令来执行,可以使用队列来执行一系列的命令,也可以单独执行某个命令.该模式重点是将不同的操作封装为不同的命令对象,将操作的调用者与执行者 ...

  9. Linux网络——配置网络之ifconfig家族命令

    Linux网络——配置网络之ifconfig家族命令 摘要:本文主要学习了ifconfig家族用来配置网络的命令. ifconfig命令 ifconfig命令用来显示或设置网络接口信息,设置只是临时生 ...

  10. 从实例一步一步入门学习SpringCloud的Eureka、Ribbon、Feign、熔断器、Zuul的简单使用(附代码下载)

    场景 SpringCloud -创建统一的依赖管理: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102530574 Sprin ...