题目:

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. (Medium)

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.

分析:

上一题的follow-up,加上了障碍物,思路一样,只是障碍物处dp[i][j] = 0;

代码:

 class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[].size();
int dp[m][n];
memset(dp,,sizeof(dp));
for (int i = ; i < n; ++i) {
if (obstacleGrid[][i] == ) {
break;
}
dp[][i] = ;
}
for (int i = ; i < m; ++i) {
if (obstacleGrid[i][] == ) {
break;
}
dp[i][] = ;
}
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (obstacleGrid[i][j] == ) {
dp[i][j] = ;
}
else {
dp[i][j] = dp[i - ][j] + dp[i][j - ];
}
}
}
return dp[m - ][n - ];
}
};

注:为什么这道题里dp[m][n] = {0}不能把二维数组初始化为0了......

LeetCode63 Unique Paths II的更多相关文章

  1. leetcode-63. Unique Paths II · DP + vector

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

  2. Leetcode63.Unique Paths II不同路径2

    一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为" ...

  3. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

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

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

  5. 【leetcode】Unique Paths II

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

  6. 61. Unique Paths && Unique Paths II

    Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...

  7. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  8. 【LeetCode练习题】Unique Paths II

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

  9. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

随机推荐

  1. 转var,let,const,js严格模式的详解

    最近看微信公众账号/知乎网上的文章说,现在的前端的人都注重用什么框架,一问原生js感觉都没有用到工作中.用不到的,学这些意义没有.上午我刚面试了一个前端,工作4年吧.最初是北大青鸟培训的,做后端.ne ...

  2. uni-app开发的应用(小程序,app,web等),使用Node+Koa2开发的后端程序接收上传文件的方法

    uni-app使用使用Node+Koa2开发的后端程序接收上传的文件 通过gitbook浏览此随笔 通过其它客户端上传(h5,小程序等),接收方法一致 使用koa接收时,我们需安装一个中间件koa-b ...

  3. [转]IE userData

    IE浏览器实现了它专属的客户端存储机制——“userData”.userData可以实现一定量的字符串数据存储,可以将其用做是Web存储的替代方案.本文将详细介绍IE userData 概述 在IE5 ...

  4. Hackerrank--Team Formation

    题目链接 For an upcoming programming contest, Roy is forming some teams from the n students of his unive ...

  5. redis为什么快

    今天面试的时候被问到的一个问题,大致说了几点.回去又研究了一下. 大致分为几点: 1:Redis是纯内存数据库,一般都是简单的存取操作,线程占用的时间很多,时间的花费主要集中在IO上,所以读取速度快. ...

  6. idea使用docker插件

    idea使用docker插件 接着上一篇docker开启远程访问后,我们就可以通过idea使用docker插件把项目部署到docker了. 首先我们先在idea安装docker插件: 在setting ...

  7. spring boot 的 ApplicationContext 及 getbean

    在spring中,我们通过如下代码取得一个spring托管类: ApplicationContext ac = new FileSystemXmlApplicationContext("ap ...

  8. update当根据条件不同时 更新同一个字段的方法 或多表插入

    1.通过存储过程 循环 传值 create or replace procedure p_u isbegin for rs in (select distinct (rks) from rkbz)lo ...

  9. 1、jxl导入/导出excel案例,黏贴即可运行

    package junit.test; import java.io.File; import java.io.IOException; import java.util.ArrayList; imp ...

  10. vue自定义指令之拖动页面的元素

    此案例中,用到了鼠标事件onmousedown.onmousemove.onmouseup 源代码如下: <!doctype html><html lang="en&quo ...