63. Unique Paths II 动态规划
description:
https://leetcode.com/problems/unique-paths/
机器人从一堆方格的左上角走到右下角,只能往右或者往下走 ,问有几种走法,这个加了难度,在矩阵中加了障碍物
Note:
Example:
Example 1:
Input:
[
[0,0,0],
[0,1,0],
[0,0,0]
]
Output: 2
Explanation:
There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
1. Right -> Right -> Down -> Down
2. Down -> Down -> Right -> Right
answer:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if (obstacleGrid.empty() || obstacleGrid[0].empty() || obstacleGrid[0][0] == 1) return 0;
int m = obstacleGrid.size(), n = obstacleGrid[0].size();
vector<vector<long>> dp(m + 1, vector<long>(n + 1, 0)); //比实际大一圈是为了处理左边和上边两个边的边缘问题
dp[0][1] = 1; // 初始化
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (obstacleGrid[i - 1][j - 1] != 0) continue; //如果是障碍则略过
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m][n];
}
};
relative point get√:
hint :
动态规划
63. Unique Paths II 动态规划的更多相关文章
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 63. Unique Paths II(有障碍的路径 动态规划)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [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 ...
- LeetCode OJ 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【LeetCode】63. Unique Paths II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- leetcode 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
随机推荐
- HTML 标签隐藏占用空间与不占用空间(Day_29)
老是有些忘记或者搞混淆,今天写篇博客. 隐藏占用空间: 将标签的属性设置为: visibility:hidden; <input id="modifId" type=&quo ...
- JavaScript 实现简易版贪吃蛇(Day_13)
时光永远在变迁,你始终要丢下过去. 使用语言 JavaScript 概述 运用JavaScript 实现简易版<贪吃蛇>. Html 页面 1 <!DOCTYPE htm ...
- centos ping命令找不到服务
1 首先 添加dns服务器 vi /etc/resolv.conf 在文件中添加如下两行: nameserver 8.8.8.8 nameserver 8.8.4.4 保存退出,重启服务器.之后再pi ...
- js获取cookie数据并发送给服务端
js获取cookie数据并发送给服务端 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...
- 书列荐书 |《刻意练习》安德斯·艾利克森,罗伯特·普尔著
花了两天的时间,一气呵成的读完了这本书.凝练的精华就是:首先,世界上并没有真正的天才这一说.基因可能会起作用,但是经过后天大量的刻意练习,基因的这种作用会弱化.刻意练习需要专注.及时的反馈,并根据反馈 ...
- 国内外企业竞争AR HUD
国内外企业竞争AR HUD 华为X红旗合作车型首曝:搭载华为AR HUD.智能座舱方案 2021年4月18日,上海国际车展正式开放,华为也成了此次车展上的重要亮点之一. 据相关报道显示,华为除了联手北 ...
- ONNX MLIR方法
ONNX MLIR方法 MLIR中的开放式神经网络交换实现. Prerequisites gcc >= 6.4 libprotoc >= 3.11.0 cmake >= 3.15.4 ...
- 毫米波RADAR与LIDAR探秘
毫米波RADAR与LIDAR探秘 说起激光雷达和毫米波雷达,相信业内人士并不陌生,激光雷达是以发射激光束探测目标的位置.速度等特征量的雷达系统.而毫米波雷达是指工作在毫米波波段探测的雷达.毫米波实质上 ...
- python+selenium基础篇,切入切出frame
1.首先制作一个html的文件,代码如下 <!DOCTYPE html> <html> <head> <title>Frame_test</tit ...
- P1024 [NOIP2001 提高组] 一元三次方程求解
题目描述 有形如:a x^3 + b x^2 + c x + d = 0 这样的一个一元三次方程.给出该方程中各项的系数(a,b,c,d均为实数),并约定该方程存在三个不同实根(根的范围在 -100至 ...