Unique Paths II ——LeetCode
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.
跟上一个题类似。
题目大意:左上走到右下,1表示障碍,0表示路,问有多少种唯一的走法。
解题思路:简单的动规,F(i,j)=F(i-1,j)+F(i,j-1),如果grid[i][j]==1,那么F(i,j)=0。
public class Solution {
public int uniquePathsWithObstacles(int[][] og) {
if(og==null||og[0].length==0||og[0][0]==1){
return 0;
}
int m = og.length,n=og[0].length;
int[][] cnt = new int[m][n];
cnt[0][0]=1;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if((i==0&&j==0)||og[i][j]==1){
continue;
} else if(i==0&&j!=0){
cnt[i][j]=cnt[i][j-1];
} else if(j==0&&i!=0){
cnt[i][j]=cnt[i-1][j];
} else {
cnt[i][j]=cnt[i-1][j]+cnt[i][j-1];
}
}
}
return cnt[m-1][n-1];
}
}
Unique Paths II ——LeetCode的更多相关文章
- Unique Paths II [LeetCode]
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- Unique Paths II leetcode java
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
随机推荐
- AS插件开发 RemoveButterKnife从构思到实现
ReomveButterKnife插件 这是一个用于移除代码中对ButterKnife使用的AS插件,接下来我们将从头开始讲讲这个插件的开发过程 地址是 https://github.com/u3sh ...
- gulp的点点滴滴
去年用gulp,但一直没有写篇博客,今天有时间无聊写一篇.... 什么是gulp?gulp的官网title上对这个工具有一个比较准确的定义,叫做:基于流的自动化构建工具.如果你查看它的网页源代码,还会 ...
- 45种Javascript技巧大全
JavaScript是一个绝冠全球的编程语言,可用于Web开发.移动应用开发(PhoneGap.Appcelerator).服务器端开发(Node.js和Wakanda)等等.JavaScript还是 ...
- JAVAWEB 生成excel文字在一格显示两位不变成#号
在用java生成excel的时候会发现这种问题, 如果是人家给的模板还好,如果不是模板,而是通过代码生成的话, 就需要进行处理了, 一个小单元格,如果是一位的话,如1-9显示没有问题,一旦是两位的话, ...
- Android开发app如何设定应用图标下的应用名称为汉字以及自定义图标
一.应用名称为汉字 二.自定义图标
- [转]一个备份MySQL数据库的简单Shell脚本
本文翻译自 iSystemAdmin 的 <A Simple Shell Script to Backup MySQL Database> Shell脚本是我们写不同类型命令的一种脚本,这 ...
- VisualStudio2013&VS2015内置SQLServer入门 (三)
关于LocalDB的部署(publish): 使用本机做服务器(目测不可行) 双击项目的Properties-->Publish-->Application Files,你会发现没有.md ...
- 【转】WF4.0实战系列索引
转自:http://www.cnblogs.com/zhuqil/archive/2010/07/05/wf4-in-action-index.html 此系列的工作流文件案例比较多点,实用性好. W ...
- Lua-C交互函数
lua_gettable(lua_State * , tableIndex) //获取表的在key位置的值 过程:tableIndex为表在栈的位置,例:-2为第二个位置 , 此时会弹(出)栈作为参数 ...
- cuda中时间用法
转载:http://blog.csdn.net/jdhanhua/article/details/4843653 在CUDA中统计运算时间,大致有三种方法: <1>使用cutil.h中的函 ...