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 ...
随机推荐
- 为什么要用redux?
redux解决的是react组件之间通信以及项目的状态变量的保存问题.如果不使用redux,随着项目的越来越大,组件之间通信越来越复杂,组件的状态越来越多,项目将越来越难以维护.使用 ...
- PaddlePaddle推理部署
PaddlePaddle推理部署 飞桨推理产品简介 作为飞桨生态重要的一部分,飞桨提供了多个推理产品,完整承接深度学习模型应用的最后一公里. 整体上分,推理产品主要包括如下子产品 各产品在推理生态中的 ...
- Jittor 的Op, Var算子
Jittor 的Op, Var算子 要使用jittor训练模型,需要了解两个主要概念: Var:Jittor的基本数据类型 Operations:Jittor的算子与numpy类似 首先,开始使用Va ...
- 编译原理-DFA的化简(最小化)
对于给定的DFA M,寻找一个状态数比M小的DFA M'使得L(M)=L(M') 1.状态的等价性: 假设s和t为M的两个状态 ①若分别从状态s和状态t出发都能读出某个字α而停止于终态,则 ...
- Telnet查看端口是否通
1. 查看端口是否通畅 telnet IP 端口号 如:telnet 10.150.159.71 5516 2.查看本机是否开启某个端口:netstat -an |findstr "端口 ...
- SpringCloud-OAuth2(三):进阶篇
上篇文章讲了SpringCloud OAuth 的实战篇,但是在微服务环境下,常常会有一个认证中心. 而普通服务接收到请求后,判断token是否有效并不是自己处理的,因为token的管理统一交给认证中 ...
- sql数据库新建作业,新建步骤时报错从 IClassFactory 为 CLSID 为 {AA40D1D6-CAEF-4A56-B9BB-D0D3DC976BA2} 的 COM 组件创建实例失败,原因是出现以下错误: c001f011。 (Microsoft.SqlServer.ManagedDTS)
简单粗暴的重启sql数据库 其他网上找的方法 32位操作系统: 打开运行(命令提示符), 一.输入 cd c:\windows\system32 进入到c:\windows\system32路径中 二 ...
- Task异常捕获的方式
这节来讲一下如果捕获Task的异常. 当Task运行中出现了异常,正常情况下我们在主线程的Try是捕获不到的,而如果在Task内部写try,出现了异常我们会完全不知道.下面就来介绍几个主线程捕获Tas ...
- 『无为则无心』Python基础 — 13、Python流程控制语句(条件语句)
目录 1.流程控制基本概念 2.选择结构(条件语句) (1)条件语句概念 (2)if语句语法 (3)if...else...语句 (4)多重判断 (5)if语句嵌套 3.应用:猜拳游戏 4.三元运算符 ...
- 01-ubuntu18.04安装docker脚本[含自动配置apt]
01-ubuntu18.04安装docker脚本[含自动配置apt] 脚本一键安装docker,并配置阿里云的docker镜像加速. #!/bin/bash #更新apt源为清华源 echo &quo ...