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 ...
随机推荐
- HTML5表单提交和PHP环境搭建
HTML5表单提交相关内容和PHP环境搭建需要的软件(只备注) (2)举例介绍 (3)PHP环境搭建
- 使用java注解的例子有没有
使用java注解的例子 参考文档:http://www.cnblogs.com/pepcod/archive/2013/02/20/2918719.html http://www.shaoqun.co ...
- Axiom3D学习日记 3.Cameras, Lights, and Shadows
Camera 相机: 相机基础知识不写了,需要注意的是:axiom目前不支持同时操作多个相机. 创建,设置位置基本操作. _camera = _scene.CreateCamera("Mai ...
- windows sever 2008 r2 - 限制ip访问
和win 7 旗舰版不同,该操作系统在安装IIS后,非本机的并不能直接访问主机.需要设置主机上的本机的IIS中的IP地址和域限制. 由于我是想在同一个局域网(路由器)中,通过Android操作系统访问 ...
- [转帖]MATLAB曲线绘制及颜色类型
信号源产生的方法 来源:http://www.2cto.com/kf/201401/270494.html matlab的checkerboard说明,GOOD! 来源:http://www.chi ...
- 对xml操作
已知有一个XML文件(bookshop.xml)如下: <?xml version="1.0" encoding="gb2312" ?> <b ...
- ASP.NET Excel数据导出数据库
/// <summary> /// 根據gridview導出excel /// </summary> /// <param name="ctl"> ...
- Python:标准数据类型6种
#!/usr/bin/python3 #python的基本语法和数据类型 #python3中 一行有多个语句,用分号分割(;) print("aaa") ;print(" ...
- window 配置 sendmail
从http://glob.com.au/sendmail/下载sendmail.zip 解压sendmail.zip到目录下(最好使用短路径,长路径会导致问题的出现),我安装的路径是: E:\wamp ...
- ThinkPHP接入支付宝支付功能
最近做系统,需要实现在线支付功能,毫不犹豫,选择的是支付宝的接口支付功能.这里我用的是即时到帐的接口,具体实现的步骤如下: 一.下载支付宝接口包 下载地址:https://b.alipay.com/o ...