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 ...
随机推荐
- centos7 安装python3.6
•到python官网找到下载路径, 用wget下载 wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz •解压tgz包 tar ...
- ZFIR_001 ole下载
*&---------------------------------------------------------------------** Report ZFIR_001* Appli ...
- 剑指offer 面试20题
面试20题: 题目:表示数值的字符串 题:请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100","5e2","-123 ...
- time函数计算时间
学过C语言的都知道有个time函数可以计算时间, 也好像知道time(NULL)返回的是一个距离1970年1月1日0时0分0秒的秒数. #include <stdio.h> #includ ...
- nginx负载均衡详情
负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...
- P4501 [ZJOI2018]胖
题目 P4501 [ZJOI2018]胖 官方口中的送分题 做法 我们通过手玩(脑补),\(a_i\)所作的贡献(能更新的点)为:在\(a_i\)更新\(\forall x\)更新前前没有其他点能把\ ...
- CentOS 6.5 下vim 配置
1. 登录并进入你常用的用户名下,查看其主目录 命令: # su xxx $ cd xxx $ ls -a 2.查看并建立目录和文件 首先看你的主目录~/ 下是否有.vimrc文件,没有就输入指令 $ ...
- 用nodejs实现读取文件操作
//如果不是全局就得引入fs成员 const fs = require("fs"); //fs 核心模块中提供了一个 fs.readFile方法,来读取指定目录下的文件 //fs. ...
- 【转】linux驱动开发
转自:http://www.cnblogs.com/heat-man/articles/4174899.html 首先理一理驱动/内核/应用程序的一些概念,以前总没有具体的去关注过! 我们的pc直观来 ...
- INSPIRED启示录 读书笔记 - 第29章 大公司如何创新
大公司实现创新的方法 20%法则:谷歌的程序员有20%的工作时间可以用来从事创新研究,这个方法最早是从施乐帕克研究所学来的.20%法则鼓励普通员工自己尝试各种想法,让员工打心底愿意倾注更多的激情和汗水 ...