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.

  1. [
  2. [0,0,0],
  3. [0,1,0],
  4. [0,0,0]
  5. ]

这个与前面那个题目基本上差不多,具体就是多了障碍,实际上遇到障碍的话把到该点的路径的数目置为0就可以了,其他的基本上与前面相同:

  1. class Solution {
  2. public:
  3. int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
  4. if(obstacleGrid.size() == || obstacleGrid[].size() == ) return ;
  5. vector<vector<int>> res(obstacleGrid.size(), vector<int>(obstacleGrid[].size(), ));
  6. int maxHor = obstacleGrid.size();
  7. int maxVer = obstacleGrid[].size();
  8. res[][] = obstacleGrid[][] == ? : ;
  9. for(int i = ; i < maxHor; ++i){
  10. res[i][] = obstacleGrid[i][] == ? : res[i - ][];
  11. }
  12. for(int j = ; j < maxVer; ++j){
  13. res[][j] = obstacleGrid[][j] == ? : res[][j - ];
  14. }
  15. for(int i = ; i < maxHor; ++i){
  16. for(int j = ; j < maxVer; ++j){
  17. res[i][j] = obstacleGrid[i][j] == ? : res[i - ][j] + res[i][j - ];
  18. }
  19. }
  20. return res[maxHor - ][maxVer - ];
  21. }
  22. };

java版本的代码如下所示:

  1. public class Solution {
  2. public int uniquePathsWithObstacles(int[][] obstacleGrid) {
  3. if(obstacleGrid.length == 0 || obstacleGrid[0].length == 0)
  4. return 0;
  5. int [][] grid = new int [obstacleGrid.length][obstacleGrid[0].length];
  6. grid[0][0] = obstacleGrid[0][0]==1 ? 0 : 1;
  7. for(int i = 1; i < obstacleGrid.length; ++i){
  8. grid[i][0] = obstacleGrid[i][0]==1? 0 : grid[i-1][0];
  9. }
  10. for(int i = 1; i < obstacleGrid[0].length; ++i){
  11. grid[0][i] = obstacleGrid[0][i]==1? 0 : grid[0][i-1];
  12. }
  13. for(int i = 1; i < obstacleGrid.length; ++i){
  14. for(int j = 1; j < obstacleGrid[0].length; ++j){
  15. grid[i][j] = obstacleGrid[i][j]==1? 0 : (grid[i-1][j] + grid[i][j-1]);
  16. }
  17. }
  18. return grid[obstacleGrid.length-1][obstacleGrid[0].length-1];
  19. }
  20. }

LeetCode OJ:Unique Paths II(唯一路径II)的更多相关文章

  1. LeetCode OJ:Unique Paths(唯一路径)

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

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

  3. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

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

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

  5. LeetCode(63):不同路径 II

    Medium! 题目描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“F ...

  6. 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】

    [062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...

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

  8. C#LeetCode刷题之#63-不同路径 II​​​​​​​(Unique Paths II)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3682 访问. 一个机器人位于一个 m x ...

  9. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

随机推荐

  1. 集成富文本编辑器XSS预防过滤措施

    # https://github.com/phith0n/python-xss-filter import re import copy from html.parser import HTMLPar ...

  2. XML和解析

    XML和解析 1.什么是XML?Extensible Markup Language,可扩展标记语言.一般也叫XML文档.和JSON一样,也是常用的一种用于交互的数据格式. 2.XML语法1)一个常见 ...

  3. socket()模块和套接字对象的内建方法

    一.socket()模块函数 要使用socket.socket()函数来创建套接字,其语法如下: socket(socket_family,socket_type,protocol=0) 如上所述,s ...

  4. 04 Spring框架 依赖注入(一)

    整理了一下之前学习spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节我们讲了几个bean的一些属性,用来限制我们实例创建过后的状态. 但是细心的我们会发现其实上面demo创建的实例并 ...

  5. java生成字符串的MD5值

    下面的代码实现了MD5值的生成: public class MD5Test2 { public static void main(String[] args) { System.out.println ...

  6. 标准输出:1>,2>,1>&2,2>&1

    在 shell 程式中,最常使用的 FD (file descriptor) 大概有三个, 分别是: 0 是一个文件描述符,表示标准输入(stdin)1 是一个文件描述符,表示标准输出(stdout) ...

  7. Ubuntu: 无法使用su命令

    Ubuntu 无法使用su命令解决方案 在Ubuntu上编译Qt环境时发现无法使用su命令切换到root用户,通过网上查找发现解决方案如下: xt@xt-ubuntu:~$ su密码: su:认证失败 ...

  8. Docker 跨主机网络

    Docker提供两种原生的跨主机网络: Overlay  和  Macvlan libnetwork & CNM libnetwork 是 docker 容器网络库,最核心的内容是其定义的 C ...

  9. JSP--常用指令

    1.JSP中的page指令: jsp中指令格式:<%@   指令名字    key=value    key=value   ......%> <%@ page language=& ...

  10. 用javascript实现的验证码

    <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">     ...