题目:

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. web服务器--nginx简介

    nginx 介绍Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务.Nginx是一款轻量级的Web 服务器/反向代理服务器及电 ...

  2. spring深入学习(六)-----springmvc

    MVC设计模式 有过一定开发经验的人肯定都知道这个模式,先简单介绍下这种模式,然后再去讨论为啥要这么设计: 传统的web应用中应该主要包括这些组件,不同组件负责不同的模块. 数据实体:POJO 数据层 ...

  3. springmvc 传参Required String parameter 'xxxx' is not present

    报错 请求因该是已经被分配了,但是参数补全,无法被执行 加上这个参数就好了,表示请求参数,可以为空 这样的好处是,可以进入controller之后再去判断,比较好定位错误

  4. CSS(前)篇

    1.1CSS重点总结 1.1.1 选择器 1.1.2 盒子模型 1.1.3 浮动 1.1.4定位 1.2CSS介绍 概念: 层叠样式表或者级联样式表(Cascading Style Sheets) 层 ...

  5. 解决pycharm新建工程项目都需要重新安装库问题

    在新建一个工程项目后,发现之前安装的库均不在了.想要使用的情况下还得重新安装.这样也太不智能了,一定会有解决的办法.查找相关文档后,发现在新建工程项目的时候需要进行选择是否使用以前的环境. 选择已存在 ...

  6. selenium(5):常用的8种元素定位

    selenium的webdriver提供了18种(注意不是8种)的元素定位方法,比较常用的定位方法是如下8种,xpath和css定位更加灵活,需要重点掌握其中一个. 经常会用到的8种定位:1.id定位 ...

  7. 【JZOJ3216】【SDOI2013】淘金

    ╰( ̄▽ ̄)╭ 小 Z在玩一个 叫做<淘金者>的游戏.游戏的世界是一个 二维坐标 .X轴.Y轴坐标范围均为1..N.初始的时候,所有的整数坐标点上均有一块金子,共 N*N 块. 一阵风吹过 ...

  8. Win7下IIS的安装配置与文件发布

    出于兴趣,抽空弄了一下Windows上的IIS,把过程记录下来与大家分享.高手请略过... 一.安装IIS.打开控制面板,单击“程序与功能” 二.点击左侧“打开或关闭Windows功能” 三.找到“I ...

  9. FZU 1575 小学生的游戏【模拟二分】

    某天,无聊的小斌叫上几个同学玩游戏,其中有比较笨的小兴,比较傻的小雪,可爱的小霞和自以为是的小楠.他们去找聪明的小明去给他们当裁判.判定谁取得游戏胜利. 而这个游戏是由小斌想个1到10000000的数 ...

  10. 三.BP神经网络

    BP神经网络是包含多个隐含层的网络,具备处理线性不可分问题的能力.以往主要是没有适合多层神经网络的学习算法,,所以神经网络的研究一直处于低迷期. 20世纪80年代中期,Rumelhart,McClel ...