题目:

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. KVO And KVC

    http://www.cocoachina.com/industry/20140224/7866.html

  2. 《程序员思维修炼》读书笔记——week4

    <程序员思维修炼>读书笔记——week4 PB16061441 陈昶金 这周读的是Andy Hunt的著作<程序员思维修炼>,这本书对于我这种刚刚入门的新手很友好,大多是讲一些 ...

  3. 谈谈Python中对象拷贝

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

  4. xampp下bugfree部署

    以Bugfree3.0.4为例,讲解如何搭建LAMP架构的Web服务器. Bugfree是一个XAMPP架构的网站,XAMPP(Apache+MySQL+PHP+PERL)是一个功能强大的搭建XAMP ...

  5. [JLOI2011]飞行路线 (分层图,最短路)

    题目链接 Solution 建立 \(k+1\) 层图跑 \(Dijkstra\) 就好了. Code #include<bits/stdc++.h> #define ll long lo ...

  6. C#创建Graphics对象的方法

    方法一.利用控件或窗体的Paint事件中的PainEventArgs 在窗体或控件的Paint事件中接收对图形对象的引用,作为PaintEventArgs(PaintEventArgs指定绘制控件所用 ...

  7. javascript事件捕获机制,dom tree

    $(document,"a").on("click",function(){alert(2);return false;}); $("<a> ...

  8. 标准C程序设计七---75

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  9. Delphi 之Copyrect的使用

    http://cqujsjcyj.iteye.com/blog/380970 Copyrect的使用(图片复制.放大.以及做图片放大镜等)一.从一个选取一个区域中的图象到另一个图象组件中的固定区域pr ...

  10. [Web Tools] 实用的Web开发工具

    模拟http请求:Postman https://www.getpostman.com 生成Json数据 http://www.json-generator.com/