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.

 class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int rows = obstacleGrid.length;
int cols = obstacleGrid[0].length;
int[][] dp = new int[rows][cols]; for(int i = 0; i < rows;i++){
for (int j = 0; j < cols ;j++){
if(obstacleGrid[i][j]==1)
dp[i][j] = 0;
else{
if (i==0&& j==0)
dp[i][j] = 1;
else if (i==0)
dp[i][j] = dp[i][j-1]; //边界 else if (j == 0)
dp[i][j] = dp[i-1][j];
else
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
}
return dp[rows-1][cols-1];
}
}
 class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int rows = obstacleGrid.length;
int cols = obstacleGrid[0].length;
for(int i = 0; i < rows;i++){
for (int j = 0; j < cols ;j++){
if(obstacleGrid[i][j]==1)
obstacleGrid[i][j] = 0;
else if (i==0&& j==0)
obstacleGrid[i][j] = 1;
else if (i==0)
obstacleGrid[i][j] = obstacleGrid[i][j-1]*1; //边界,没有路径了,要么是0,要么是1 else if (j == 0)
obstacleGrid[i][j] = obstacleGrid[i-1][j]*1;
else
obstacleGrid[i][j] = obstacleGrid[i-1][j] + obstacleGrid[i][j-1];
}
}
return obstacleGrid[rows-1][cols-1];
}
}

63. Unique Paths II(有障碍的路径 动态规划)的更多相关文章

  1. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  2. [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 ...

  3. 【LeetCode】63. Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

  4. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  5. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

  6. 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). ...

  7. leetcode 63. Unique Paths II

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

  8. 63. Unique Paths II

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

  9. 63. Unique Paths II(中等, 能独立做出来的DP类第二个题^^)

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

随机推荐

  1. Unity Shaders and Effects Cookbook (3-5) 金属软高光

    书上这一节看得我头昏脑胀,数学渣表示自理不能-- 并且也不了解这个效果的实际意义. 先记录下来,后面真正看懂了再来补充具体理论. 通过一张纹理贴图,定义高光的形状,利用到的纹理贴图有三种 这里并非把纹 ...

  2. shell脚本学习总结04--终端信息的获取和设置

    tput tput 命令将通过 terminfo 数据库对您的终端会话进行初始化和操作.通过使用 tput,您可以更改几项终端功能,如移动或更改光标.更改文本属性,以及清除终端屏幕的特定区域. 文本属 ...

  3. 应用开发之Asp.net

    本章简言 上一章中笔者讲到关于Linq和EF的用法.并以hibernate来进行讲解.那么本章笔者来讲一下C#的Asp.Net.即是在B/S模式下开发.现在企业大部分的业务都是面向B/S模式的.所以对 ...

  4. cocos2dx热更新tmx的一个坑

    游戏项目中使用了tmx地图,当对tmx文件进行热更新时,进入该地图总是宕机,纠结了几小时终于发现,cocos读取tmx文件时,会默认tmx关联的图集文件和tmx在同一目录,然而那个图集文件并没有在热更 ...

  5. ajax的轮询和长轮询

    概念: 轮询(polling):客户端按规定时间定时像服务端发送ajax请求,服务器接到请求后马上返回响应信息并关闭连接. 概念总是枯燥的,只有代码方能解心头之快 前段代码:index.html: & ...

  6. 5个基于Linux命令行的文件下载和网站浏览工具

    导读 命令行是GNU/Linux中最神奇迷人的部分,它是非常强大的工具;命令行本身功能多样,多种内建或者第三方的命令行应用使得Linux变得更加健壮和强大.Linux Shell支持多种不同类型的网络 ...

  7. AngularJS 解决 SEO 问题

    由于 AngularJS 返回的是HTML模板,实际的内容需要执行JS以后才会填充进去,导致百度抓取蜘蛛抓不到,因此产生了 AngularJS 的 SEO 问题.经过几天的研究试验,我们的解决方案是这 ...

  8. poj1015 Jury Compromise【背包】

    Jury Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:32355   Accepted:8722   ...

  9. linux下安装JDK,及配置环境变量

    首先去官网https://www.oracle.com/technetwork/java/javase/downloads/index.html下载最新的JDK版本: 以下操作在root用户下操作 第 ...

  10. 不得不知的Excel技巧

    1.超链接 选中一个格右击选择超链接. 2.求和 选择一个格点击开始中的求和按钮并拖动求和区域. 3.冻结 冻结一行,选择一行区域,选择开始菜单中的冻结窗格. 冻结上面的行和左边的行,选择夹角的格并点 ...