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. Mysql查询结果只有一条的情况下把值赋值给变量,再用if else 流程判断

    BEGIN set @n=(SELECT count(day) from log where day=CURDATE()); THEN call m_LogInsert(); ELSE call m_ ...

  2. Java编程手冊-Collection框架(上)

    该文章所讲内容基本涵盖了Collection里面的全部东西,尽管基于jdk 1.5的.可是思路非常清晰 1.引言 1.1 Collection框架的介绍 尽管我们能够使用数组去存储具有同样类型的元素集 ...

  3. call_user_func — 把第一个参数作为回调函数调用

    call_user_func — 把第一个参数作为回调函数调用 说明 mixed call_user_func ( callable $callback [, mixed $parameter [, ...

  4. PyQt5 GUI Programming With Python 3.6 (一)

    PyQt5 PyQt5是一个基于强大的图形程式框架Qt5的python接口, 主要包含以下几个大类: ● QtCore ● QtGui ● QtWidgets ● QtMultimedia ● QtB ...

  5. C#中YieldReturn的用法

    Yield Return 和 Yield Break 的出现是为了简化迭代器. 类如果能被遍历则必须有IEnumerator<string> GetEnumerator() 方法, 并有用 ...

  6. u-boot README--Memory Management&initialize

    Memory Management:------------------ U-Boot runs in system state and uses physical addresses, i.e. t ...

  7. Hibernate3中generator标签使用

    在XXX.hbm.xml映射配置文件中定义<generator class="native"/>标签是为了告诉Hibernate框架使用什么方式去生成主键class对应 ...

  8. strpos 判断字符串是否存在

    strpos    中为什么要用逗号隔开的原因是因为   防止找出相匹配的中 ,  如   查找1    而数组中  存在  12  那么这个结果也是可以找出来的 ,分别在1 前后加个,  就是为了区 ...

  9. UFLDL深度学习笔记 (七)拓扑稀疏编码与矩阵化

    UFLDL深度学习笔记 (七)拓扑稀疏编码与矩阵化 主要思路 前面几篇所讲的都是围绕神经网络展开的,一个标志就是激活函数非线性:在前人的研究中,也存在线性激活函数的稀疏编码,该方法试图直接学习数据的特 ...

  10. 【文献阅读】Deep Residual Learning for Image Recognition--CVPR--2016

    最近准备用Resnet来解决问题,于是重读Resnet的paper <Deep Residual Learning for Image Recognition>, 这是何恺明在2016-C ...