题目:

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. python爬虫初认识

    一.爬虫是什么? 如果我们把互联网比作一张大的蜘蛛网,数据便是存放于蜘蛛网的各个节点,而爬虫就是一只小蜘蛛, 沿着网络抓取自己的猎物(数据)爬虫指的是:向网站发起请求,获取资源后分析并提取有用数据的程 ...

  2. SSM框架(Spring + Spring MVC + Mybatis)搭建

    Spring是一个轻量级的框架,用到了注解和自动装配,就是IOC和AOP: SpringMVC是Spring实现的一个Web层,相当于Struts的框架: Mybatis是 一个持久层的框架,在使用上 ...

  3. HTML页面导入模板页面(Tomcat)

    找了个前端模板,多个HTML中有重复的部分,一改都改,所以对其进行重构,将重复的部分拿出来 看了很多方法,最简单的是jQuery,但是在我这没起作用,后来发现一个配置tomcat的ssi,让服务器帮我 ...

  4. django--中运行scrapy框架

    1.新建一个django项目, 2.前端展示一个按钮 <form action="/start/" method="POST"> {% csrf_t ...

  5. Loj #6503. 「雅礼集训 2018 Day4」Magic

    Loj #6503. 「雅礼集训 2018 Day4」Magic 题目描述 前进!前进!不择手段地前进!--托马斯 · 维德 魔法纪元元年. 1453 年 5 月 3 日 16 时,高维碎片接触地球. ...

  6. WPF 使用SetParent嵌套窗口

    有点类似与Winform的MDI窗口. 使用函数为SetParent和MoveWindow(经常配合). [DllImport("user32.dll", SetLastError ...

  7. pandas 学习 第6篇:DataFrame - 数据处理(长宽格式、透视表)

    长宽格式的转换 宽格式是指:一列或多列作为标识变量(id_vars),其他变量作为度量变量(value_vars),直观上看,这种格式的数据比较宽,举个列子,列名是:id1.id2.var1.var2 ...

  8. Elasticsearch Query DSL 语言介绍

    目录 0. 引言 1. 组合查询 2. 全文搜索 2.1 Match 2.2 Match Phase 2.3 Multi Match 2.4 Query String 2.5 Simple Query ...

  9. Wpf Prism.Unity 7

    Prism.Unity 中UnityBootStrapper已经不用了,可以继承PrismApplication 1.Install-package Prism.Unity -v 7.2.0.1367 ...

  10. indexOf()字符位置

    package seday01; /** * int indexOf(String str) * 查找给定字符串在当前字符串中的位置,若返回值为-1,则 * 表示当前字符串中不含有给定的内容. * @ ...