[LeetCode][Java] Unique Paths 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]
]
The total number of unique paths is 2
.
Note: m and n will be at most 100.
题意:
紧跟着题目《Unique Paths》,现给出这样一题目:
假设在格子中加入一些障碍,会出现多少存在且唯一的不同路径呢?
障碍和空白格子分别被标记为1
and 0
.
比方一个3x3的格子中的中间存在一个障碍,例如以下所看到的:
[
[0,0,0],
[0,1,0],
[0,0,0]
]
总的路径数为2.
算法分析:
思路与题目《Unique Paths》类似,不同之处为:
初始化边界上行和列时,出现障碍。后面路径数dp的都是0
中间的格子出现障碍时,该格子dp表示的路径数直接填0
AC代码:
public class Solution
{
public int uniquePathsWithObstacles(int[][] obstacleGrid)
{
if(obstacleGrid==null||obstacleGrid.length==0)
return 0;
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int [][] dp = new int[m][n];
for(int i = 0; i < m; i++)
{
if(obstacleGrid[i][0]!=1)
dp[i][0] = 1;
else
break;
}
for(int j = 0; j < n; j++)
{
if(obstacleGrid[0][j]!=1)
dp[0][j] = 1;
else
break;
}
for(int i = 1; i < m; i++)
{
for(int j = 1; j< n; j++)
{
if(obstacleGrid[i][j]!=1)
dp[i][j] = dp[i-1][j] + dp[i][j-1];
else
dp[i][j]=0;
}
}
return dp[m-1][n-1];
}
}
[LeetCode][Java] Unique Paths II的更多相关文章
- 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 Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- Java for LeetCode 063 Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [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 ...
- leetcode 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【题解】【矩阵】【回溯】【Leetcode】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 ...
- leetcode 【 Unique Paths II 】 python 实现
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- LeetCode: 63. Unique Paths II(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/
随机推荐
- 深入了解ASO
ASO对于一些人来说可能很陌生,很多人都听说过SEO,没有听说过ASO(我也是最近才知道这个领域),因为这是一个数字营销的一个新领域.ASO(App Store Optimization)是为了让自己 ...
- Java项目引入eclipse注意事项
我以前也搞过java,后转前端,接触html+css+js时间比较多,所以java后端也忘了差不多.最近负责公司的邮件系统项目,项目是java语言写,项目架构比较复杂,在部署项目的时候,遇到了很多问题 ...
- html 标签附加文本属性
<!DOCTYPE html> <html> <head> <script> function showDetails(animal) { var an ...
- django 的序列化
关于Django中的序列化主要应用在将数据库中检索的数据返回给客户端用户,特别的Ajax请求一般返回的为Json格式. 我们从数据库取出数据的格式有三种:1.all()返回的是QuerySet对象:2 ...
- Python文件处理、函数的基本应用
可读可写: r+t:可读.可写 w+t:可写.可读with open('b.txt','w+t',encoding='utf-8') as f: print(f.readable()) p ...
- javascript学习笔记 - 执行环境及作用域
一 执行环境(环境) 1.每个执行环境都有一个关联的全局变量对象.例如:web浏览器中,window对象为全局变量对象.环境中定义的所有变量和函数都保存在该对象中.全局执行环境是最外围的环境. 2.执 ...
- sqlserver的left join优化
MS sqlserver 对4张表进行left join join字段是varchar类型长度20,也都建了索引,但是光查一个count(Id) 耗时就超过了8秒,数据量只有100多万条,该怎么优化呢 ...
- Linux问题故障定位
CPU 针对应用程序,通常关注的是内核CPU调度器功能和性能. 线程的状态分析主要是分析线程的时间用在什么地方,而线程状态的分类一般分为: a. on-CPU:执行中,执行中的时间通常又分为用户态时间 ...
- 利用json实现数据传输
JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是存储和交换文本信息的语法.类似 XML. JSON 比 XML 更小.更快,更易解析. ...
- Linux下安装Mysql出现的常见问题以及解决办法
1.安装时候出现 warning: mysql-community-server-5.7.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ...