题目:

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.

题意:

紧跟着题目《Unique Paths》,现给出这样一题目:

假设在格子中加入一些障碍,会出现多少存在且唯一的不同路径呢?

障碍和空白格子分别被标记为1 and 0 .

比方一个3x3的格子中的中间存在一个障碍,例如以下所看到的:

[
[0,0,0],
[0,1,0],
[0,0,0]
]

总的路径数为2.

算法分析:

思路与题目《Unique Paths》类似,不同之处为:

初始化边界上行和列时,出现障碍。后面路径数dp的都是0

中间的格子出现障碍时,该格子dp表示的路径数直接填0

AC代码:

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

[LeetCode][Java] Unique Paths II的更多相关文章

  1. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  2. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

  3. 【leetcode】Unique Paths II

    Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...

  4. Java for LeetCode 063 Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  5. [LeetCode] 63. Unique Paths II 不同的路径之二

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  6. leetcode 63. Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  7. 【题解】【矩阵】【回溯】【Leetcode】Unique Paths II

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  8. leetcode 【 Unique Paths II 】 python 实现

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

  9. LeetCode: 63. Unique Paths II(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/

随机推荐

  1. Jmeter测试https协议

  2. Selenium WebDriver- 显式等待

    推荐使用显示等待,元素出现就不会等待而继续执行了.节省时间. #encoding=utf-8 import unittest import time from selenium import webd ...

  3. python + selenium - 自动化环境搭建

    1. 安装python (1)下载地址:https://www.python.org/downloads/windows/ (2)安装方式:默认安装即可 (3)环境变量配置:打开[系统属性]-[环境变 ...

  4. Multi-Dimensional Recurrent Neural Networks

    Multi-Dimensional Recurrent Neural Networks The basic idea of MDRNNs is to replace the single recurr ...

  5. [python篇]学习网址--更新

    http://www.yiibai.com/python/python_modules.html  ---易白教程 http://python.jobbole.com/81477/---伯乐在线

  6. 解决vue、cnpm造成的样式错位问题

    删除node_modules文件夹使用npm install不要使用cnpm install

  7. python相关——如何安装pip

    今天在新的一台电脑上安装了pip.流程有点忘记了,在这里再次记录下来. 本教程基于python3.4,并且需要连接互联网,总共需要两步. 1.要安装pip,首先要安装setuptools,链接:htt ...

  8. 谈谈Python中对象拷贝

    你想复制一个对象?因为在Python中,无论你把对象做为参数传递,做为函数返回值,都是引用传递的. 何谓引用传递,我们来看一个C++交换两个数的函数: void swap(int &a, in ...

  9. BZOJ2333 [SCOI2011]棘手的操作 【离线 + 线段树】

    题目 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边,连接第x个节点和第y个节点 A1 x v: 将第x个节点的权 ...

  10. CentOS7 修改时区、charset

    1. 修改时区 ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 经过这番修改后,JAVA启动后自动使用了Shanghai作为时区. 2. ...