LeetCode 63. Unique Paths II
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 3×3 grid as illustrated below.
[[0,0,0],[0,1,0],[0,0,0]
]
The total number of unique paths is 2.
分析
这道题和unique path I 没有本质区别,动态转移方程是dp[i][j]=obstacleGrid[i][j]==1?0:dp[i][j-1]+dp[i-1][j];
class Solution {
public:int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {vector<vector<int>> dp(obstacleGrid.size(),vector<int>(obstacleGrid[0].size(),0));if(obstacleGrid[0][0]==1) return 0;else dp[0][0]=1;for(int i=1;i<obstacleGrid[0].size();i++)dp[0][i]=obstacleGrid[0][i]==1?0:dp[0][i-1];for(int i=1;i<obstacleGrid.size();i++)dp[i][0]=obstacleGrid[i][0]==1?0:dp[i-1][0];for(int i=1;i<obstacleGrid.size();i++)for(int j=1;j<obstacleGrid[0].size();j++)dp[i][j]=obstacleGrid[i][j]==1?0:dp[i-1][j]+dp[i][j-1];return dp[obstacleGrid.size()-1][obstacleGrid[0].size()-1];}
};
转载于:https://www.cnblogs.com/A-Little-Nut/p/10041197.html
LeetCode 63. Unique Paths II相关推荐
- 【动态规划】LeetCode 63. Unique Paths II
LeetCode 63. Unique Paths II Solution1:我的答案 在哪里做过这题? class Solution { public:int uniquePathsWithObst ...
- [Lintcode]115. Unique Paths II/[Leetcode]63. Unique Paths II
115. Unique Paths II/63. Unique Paths II 本题难度: Easy/Medium Topic: Dynamic Programming Description Fo ...
- [LeetCode]--63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 63. Unique Paths II and 64. Minimum Path Sum
文章目录 1 63 Unique Paths II 1.1 题目描述 1.2 动态规划解决 2 64. Minimum Path Sum 2.1 题目理解 2.2 动态规划 这一遍刷dp的题目就很轻松 ...
- 63. Unique Paths II
和Unique paths是一样的 1 public int uniquePathsWithObstacles(int[][] obstacleGrid) { 2 if(obstacleGrid == ...
- [LeetCode][Java] Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 63. Unique Paths II 动态规划
description: https://leetcode.com/problems/unique-paths/ 机器人从一堆方格的左上角走到右下角,只能往右或者往下走 ,问有几种走法,这个加了难度, ...
- 63. Unique Paths II 不同路径 II
Title 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为 ...
- 【动态规划】LeetCode 62. Unique Paths
LeetCode 62. Unique Paths Solution1:我的未能AC的答案 递归超时了!!! class Solution { public:int uniquePaths(int m ...
最新文章
- 字符集编码ANSI和UNICODE
- C# TCPClient简单示例
- db2-存储过程word版
- Linux部署禅道环境
- java快递追踪系统_物流跟踪(快递100)
- C++: 06---构造函数析构函数
- B. One Bomb (#363 Div.2)
- 运行报错provider = models.ForeignKey(Provider, on_delete=True) TypeError(‘on_delete must be callable.‘)
- 布谷直播:仿富聊V聊、一对一社交APP、抖音短视频、社交APP源码
- 机器学习方向企业面试题(二)
- 百度搜索引擎关键字URL采集爬虫优化行业定投方案高效获得行业流量-笔记篇
- ajax获取inputname值,jQuery - 获取ajax POST的表单值
- 【MicroPython ESP32】ssd1306 0.96“OLED+气象数据中文显示
- python做excel表格教程视频_Python玩转excel表格
- 随机抽样C语言,R语言中实现简单随机抽样
- day26 SQL注入神器—— Sqlmap
- 苹果手机自带软件删除了怎么恢复_苹果手机数据被删除如何来恢复数据???...
- 如何获取新浪微博数据
- 四川汶川县今天又连发生地震!
- VMware虚拟机配置IP地址
热门文章
- 第一 二章 计算机基础知识,[精品]第二章-计算机基础知识.doc
- 含有swap的c语言冒泡排序6,c#中写个Swap方法来实现冒泡排序 看看哪里错了
- java 对象地址_如何获取Java对象的地址?
- 深入浅出mysql gtid_Mysql 5.7 Gtid内部学习(九) 实际案例(一)
- Numpy Binary operations
- TensorFlow tf.expand_dims
- sqlalchemy Column Elements
- Linux安装包下载地址(国内)
- centos网卡发送被占满了_CentOS 查看实时网络带宽占用情况方法
- mysql proxy 读写分离_mysql-proxy 实现读写分离