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. BZOJ 1012 线段树||单调队列

    非常裸的线段树  || 单调队列: 假设一个节点在队列中既没有时间优势(早点入队)也没有值优势(值更大),那么显然不管在如何的情况下都不会被选为最大值. 既然它仅仅在末尾选.那么自然能够满足以上的条件 ...

  2. Python修改文件权限

    os.chmod()方法 此方法通过数值模式更新路径或文件权限.该模式可采取下列值或按位或运算组合之一: stat.S_ISUID: Set user ID on execution. stat.S_ ...

  3. brew Error: Formulae found in multiple taps

    Mac PHP brew install php56-apcu Error: Formulae found in multiple taps: * homebrew/php/php56-apcu * ...

  4. String、StringBuffer、StringBuilder区别并验证

    © 版权声明:本文为博主原创文章,转载请注明出处 String.StringBuffer.StringBuilder的区别 1.String是一个常量,其对象一旦创建完毕就无法改变,当使用“+”拼接字 ...

  5. CentOS7 yum 安装 Nginx最新版本

    CentOS7 yum 安装 Nginx最新版本 下载对应当前系统版本的nginx包(package) # wget  http://nginx.org/packages/centos/7/noarc ...

  6. 受防火墙影响,win7的IPV6经常Ping不通,需要手动放行

    在路由器上面设置了IPV6隧道,路由自动给每台机器分配了IPV6地址. 然后总是发现过会IPV6就ping不通了,IPV6的网站也打不开. 经从次实验发现:关闭防火墙后立马就通了,打开防火墙后,过会又 ...

  7. JavaScript的toString()

    JavaScript toString() 方法 JavaScript Boolean 对象 定义和用法 toString() 方法可把一个逻辑值转换为字符串,并返回结果. 语法 booleanObj ...

  8. Android开发和Android Studio使用教程

    Android studio安装和简单介绍http://www.jianshu.com/p/36cfa1614d23 是时候把Android 项目切换到Android Studio http://ww ...

  9. oracle高性能的SQL语句的写法

    1.当多表查询的时候,把数据量小的表放在最后面,ORACLE会把最后面的表当作基础表,因为表间连接时,最右边的表会被放到嵌套循环的最外层.最外层的循环次数越少,效率越高. 2.Oracle采用自下而上 ...

  10. mybatis 单一参数时的动态语句

    public void getBookList(String publisher,String author){ Map<String,Object> maps = new HashMap ...