LeetCode OJ:Unique Paths II(唯一路径II)
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]
- ]
这个与前面那个题目基本上差不多,具体就是多了障碍,实际上遇到障碍的话把到该点的路径的数目置为0就可以了,其他的基本上与前面相同:
- class Solution {
- public:
- int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
- if(obstacleGrid.size() == || obstacleGrid[].size() == ) return ;
- vector<vector<int>> res(obstacleGrid.size(), vector<int>(obstacleGrid[].size(), ));
- int maxHor = obstacleGrid.size();
- int maxVer = obstacleGrid[].size();
- res[][] = obstacleGrid[][] == ? : ;
- for(int i = ; i < maxHor; ++i){
- res[i][] = obstacleGrid[i][] == ? : res[i - ][];
- }
- for(int j = ; j < maxVer; ++j){
- res[][j] = obstacleGrid[][j] == ? : res[][j - ];
- }
- for(int i = ; i < maxHor; ++i){
- for(int j = ; j < maxVer; ++j){
- res[i][j] = obstacleGrid[i][j] == ? : res[i - ][j] + res[i][j - ];
- }
- }
- return res[maxHor - ][maxVer - ];
- }
- };
java版本的代码如下所示:
- public class Solution {
- public int uniquePathsWithObstacles(int[][] obstacleGrid) {
- if(obstacleGrid.length == 0 || obstacleGrid[0].length == 0)
- return 0;
- int [][] grid = new int [obstacleGrid.length][obstacleGrid[0].length];
- grid[0][0] = obstacleGrid[0][0]==1 ? 0 : 1;
- for(int i = 1; i < obstacleGrid.length; ++i){
- grid[i][0] = obstacleGrid[i][0]==1? 0 : grid[i-1][0];
- }
- for(int i = 1; i < obstacleGrid[0].length; ++i){
- grid[0][i] = obstacleGrid[0][i]==1? 0 : grid[0][i-1];
- }
- for(int i = 1; i < obstacleGrid.length; ++i){
- for(int j = 1; j < obstacleGrid[0].length; ++j){
- grid[i][j] = obstacleGrid[i][j]==1? 0 : (grid[i-1][j] + grid[i][j-1]);
- }
- }
- return grid[obstacleGrid.length-1][obstacleGrid[0].length-1];
- }
- }
LeetCode OJ:Unique Paths II(唯一路径II)的更多相关文章
- 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 ...
- [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 ...
- 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). ...
- [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 ...
- LeetCode(63):不同路径 II
Medium! 题目描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“F ...
- 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】
[062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...
- [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 ...
- C#LeetCode刷题之#63-不同路径 II(Unique Paths II)
目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3682 访问. 一个机器人位于一个 m x ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
随机推荐
- 集成富文本编辑器XSS预防过滤措施
# https://github.com/phith0n/python-xss-filter import re import copy from html.parser import HTMLPar ...
- XML和解析
XML和解析 1.什么是XML?Extensible Markup Language,可扩展标记语言.一般也叫XML文档.和JSON一样,也是常用的一种用于交互的数据格式. 2.XML语法1)一个常见 ...
- socket()模块和套接字对象的内建方法
一.socket()模块函数 要使用socket.socket()函数来创建套接字,其语法如下: socket(socket_family,socket_type,protocol=0) 如上所述,s ...
- 04 Spring框架 依赖注入(一)
整理了一下之前学习spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节我们讲了几个bean的一些属性,用来限制我们实例创建过后的状态. 但是细心的我们会发现其实上面demo创建的实例并 ...
- java生成字符串的MD5值
下面的代码实现了MD5值的生成: public class MD5Test2 { public static void main(String[] args) { System.out.println ...
- 标准输出:1>,2>,1>&2,2>&1
在 shell 程式中,最常使用的 FD (file descriptor) 大概有三个, 分别是: 0 是一个文件描述符,表示标准输入(stdin)1 是一个文件描述符,表示标准输出(stdout) ...
- Ubuntu: 无法使用su命令
Ubuntu 无法使用su命令解决方案 在Ubuntu上编译Qt环境时发现无法使用su命令切换到root用户,通过网上查找发现解决方案如下: xt@xt-ubuntu:~$ su密码: su:认证失败 ...
- Docker 跨主机网络
Docker提供两种原生的跨主机网络: Overlay 和 Macvlan libnetwork & CNM libnetwork 是 docker 容器网络库,最核心的内容是其定义的 C ...
- JSP--常用指令
1.JSP中的page指令: jsp中指令格式:<%@ 指令名字 key=value key=value ......%> <%@ page language=& ...
- 用javascript实现的验证码
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> ...