LeetCode OJ 63. 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.
【题目分析】
相比较上一个题目,在这个问题的grid中有部分区域是存在障碍物的,即有些区域不能到达。
【思路】
我们分析一下如果存在障碍物这个问题我们该如何解决。
1. 如果障碍物在入口或者出口,那么总的方法数就是0;
2. 如果障碍物在其他位置,那么存在障碍物的位置能到达的方法数为零;
在上一题中用到的方法在这个题目中也适用,只是需要判定一下当前是否是障碍物,如果是障碍物,则直接把能到达当前位置的方法数设置为0,否则:A[j] = A[j] + A[j - 1];

如上图中,黑框表示该位置存在障碍物,那么能到达这个位置的方法数只能被设置为0,然后按照动态规划的方法一行一行遍历,求出能到达每一个位置的方法数,直到求出能到达出口的方法数为止。
【java代码】
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int row = obstacleGrid.length;
int col = obstacleGrid[0].length;
if(row == 0 || col == 0) return 0;
int[] dp = new int[col];
dp[0] = 1;
for (int i = 0; i < row; i++){
if(obstacleGrid[i][0] == 1) dp[0] = 0;
for (int j = 1; j < col; j++){
if(obstacleGrid[i][j] == 1) dp[j] = 0;
else dp[j] = dp[j - 1] + dp[j];
}
}
return dp[col - 1];
}
}
LeetCode OJ 63. Unique Paths II的更多相关文章
- 【一天一道LeetCode】#63. Unique Paths II
一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 【LeetCode】63. Unique Paths II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- [leetcode DP]63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LeetCode OJ:Unique Paths II(唯一路径II)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeedCode OJ]#63 Unique Paths II
[ 声明:版权全部,转载请标明出处,请勿用于商业用途. 联系信箱:libin493073668@sina.com] 题目链接:https://leetcode.com/problems/uniqu ...
- leetcode@ [62/63] Unique Paths II
class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleG ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
随机推荐
- 中文版的jqGrid实例大全
中文版的jqGrid实例大全 http://blog.mn886.net/jqGrid/
- Java Calendar日历类的使用
Calendar cal = Calendar.getInstance(); // 当前年 int year = cal.get(Calendar.YEAR); // 当前月 int month = ...
- java操作mongodb——插入数据
在mongodb中,表(Table)被称之为集合(Collection),记录(Record)被称为文档(Document) 首先连接到数据库 MongoClient mongoClient = ne ...
- SEO之关键词选择
在网站优化中,关键词应该是奠基石,选择好关键词的重要性也不言而喻了.怎样选择合理化的关键词呢?这个是我今天了解到的. 1.选择的关键词首先是有人搜索过的.没人搜索的词优化就是浪费时间. 2.做有效流量 ...
- [ mysql优化一 ] explain解释select语句
NOSQL 没有什么数据表, 只是一些变量,key_value ,redis 支持的变量比较多.可以持久化文件到硬盘上. mysql 关系型数据库 ,表和表中间有各种id的关系. 缺点 高并发读 ...
- SQLite模糊查找(like)
select UserId,UserName,Name,Sex,Birthday,Height,Weight,Role from xqhit_Users where UserName like &qu ...
- SQLSERVER 的联接查询写法
1.内连接 语法:[JOIN.INNER JOIN] 作用:两个表相连,加上ON匹配两个表的共同条件. 实例1: SELECT tb_o_i.* FROM tb_o_i INNER JOIN tb_o ...
- 【翻译】编译Cordova项目
针对iOS创建项目 需要安装iOS SDK才能创建Workshop项目 打开终端工具并使用cd命令进入workshop目录执行下面都命令 cordova build ios 项目建立在workshop ...
- C# 手机格式验证
C# 手机格式验证 //电信手机号码正则 Regex dReg = new Regex(@"^1[3578][01379]\d{8}$"); //联通手机号正则 Regex tRe ...
- sql server 查询表基本信息sql
SELECT c.name,t.name TYPE,c.max_length,c.precision,c.scale,p.value FROM sys.systypes t INNER JOIN sy ...