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.


题解:和http://www.cnblogs.com/sunshineatnoon/p/3798167.html非常相似,要注意两点:

  1. 有障碍的地方能走到的方法数目是0;
  2. 初始化结果矩阵的时候,如果第一行(列)的某个位置有障碍,那么这一行(列)该元素后面所有的元素都是0,不能置为1;比如给定障碍矩阵[1,0,0],那么初始化以后的矩阵应该为[0,0,0],而不是[0,1,1],因为(0,0)处有障碍,那么该矩阵的任何位置都是无法到达的。

代码如下:

 public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
if(m==0 && n == 0)
return 0; int[][] PathNum = new int[m][n];
for(int i = 0;i < m;i++)
if(obstacleGrid[i][0] != 1)
PathNum[i][0] = 1;
else
break;
for(int i = 0;i < n;i++)
if(obstacleGrid[0][i] != 1)
PathNum[0][i] = 1;
else
break; for(int i = 1;i < m;i++)
for(int j = 1;j < n;j++){
if(obstacleGrid[i][j] != 1)
PathNum[i][j] = PathNum[i-1][j]+PathNum[i][j-1];
} return PathNum[m-1][n-1];
}
}

【leetcode刷题笔记】Unique Paths II的更多相关文章

  1. 【leetcode刷题笔记】Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. 【leetcode刷题笔记】Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  3. 【leetcode刷题笔记】N-Queens II

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  4. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  5. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  6. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  7. LeetCode(63)Unique Paths II

    题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. Ho ...

  8. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  9. 刷题62. Unique Paths

    一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...

随机推荐

  1. 【SpringMVC学习09】SpringMVC与前台的json数据交互

    json数据格式在接口调用中.html页面中比较常用,json格式比较简单,解析也比较方便,所以使用很普遍.在springmvc中,也支持对json数据的解析和转换,这篇文章主要总结一下springm ...

  2. listItem选中状态高亮

    两种方法1.在adapter中添加方法changeSelected()int mSelect = 0; //mSelect为选中项public void changeSelected(int posi ...

  3. python抓取51CTO博客的推荐博客的全部博文,对标题分词存入mongodb中

    原文地址: python抓取51CTO博客的推荐博客的全部博文,对标题分词存入mongodb中

  4. swift中代理的使用

    1.首先定义一份协议. protocol HttpToolProrocol{ //1.代理方法,将server返回的字典传递给调用者 func didRecieveResults(result:NSD ...

  5. 跟我一起写 Makefile(二)[转]

    原文链接 http://bbs.chinaunix.net/thread-408225-1-1.html(出处: http://bbs.chinaunix.net/) 一.Makefile里有什么? ...

  6. 篇三、开发前知识补充:Android的长度单位和屏幕分辨率,这个也是转载~~

    这篇文章有点早,不过很实用.单位的实用看最后的红色标注的部分. 屏幕分辨率基础 1.术语和概念 术语 说明 备注 Screen size(屏幕尺寸) 指的是手机实际的物理尺寸,比如常用的2.8英寸,3 ...

  7. IE 下 log 调试的一点不同

    介绍一点IE下控制台调试与chrome的区别 数组 alert([1,2]) console.log([1,2]) console.log([1,2],"1,2") alert I ...

  8. Spring整合JMS(消息中间件)

    这一节来说说,异步机制及spring对JMS封装 一.消息异步处理 类似于RMI.Hessian.Burlap等远程方法调用,它们都是同步的,所谓同步调用就是客户端必须等待操作完成,如果远程服务没有返 ...

  9. Linq系列(7)——表达式树之ExpressionVisitor

    大家好,由于今天项目升级,大家都在获最新代码,所以我又有时间在这里写点东西,跟大家分享. 在上一篇的文章中我介绍了一个dll,使大家在debug的时候可以可视化的看到ExpressionTree的Bo ...

  10. hdu 5881 Tea (2016 acm 青岛网络赛)

    原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5881 Tea Time Limit: 3000/1000 MS (Java/Others)    Me ...