LeetCode63——Unique Paths II

手把手教你解决Ubuntu 20.04虚拟机桥接模式下的网络问题:DHCP与静态IP配置全指南 本文详细解析了Ubuntu 20.04虚拟机在桥接模式下的网络配置问题,包括DHCP与静态IP的配置方法及常见故障排查技巧。通过深入讲解桥接模式原理和提供实战操作指南,帮助用户解决无法访问外网、ping不通等典型网络问题,实现虚拟机与物理网络的无缝连接。 阅读详情

LeetCode63——Unique Paths II

LeetCode62——Unique Paths不同的是,这里是要在路径上设置障碍的。

因此不能用组合的方法去做了(也许可以,但我实在想不到)

那就是传统二维数组的动态规划了。


定义动归方程如下:

首先 dp[i][j]表示到 点 (i,j) 路径的可能数,那么假设不存在障碍的情况下,则有:

dp[i][j]=dp[i-1][j] + dp[i][j-1]  因为他可以从两个不同的方向走到(i,j)处。


现在考虑障碍,如果  (i,j)  处有障碍,即值为1。那么不论dp[i-1][j] 和 dp[i][j-1]为多少,dp[i][j]恒为0


所以就有:


dp[i][j]=dp[i-1][j] + dp[i][j-1]    map[i][j]=0

dp[i][j]=0 map[i][j]=1


最后要注意的就是(0,1)  (1,0)这样的边界条件

代码:

class Solution {
public:
	int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
		int m = obstacleGrid.size();
		int n = obstacleGrid[0].size();
		if (m == 0 || n == 0)
			return 0;
		vector<vector<int>>dp(m, vector<int>(n));
		dp[0][0] = obstacleGrid[0][0] == 1 ? 0 : 1;
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (i >= 1 && j >= 1)
				{
					if (obstacleGrid[i][j] == 0)
						dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
					else
						dp[i][j] = 0;
				}
				else if (i >= 1)//考虑边界条件
				{
					if (obstacleGrid[i][j] == 0)
						dp[i][j] = dp[i - 1][j];
					else
						dp[i][j] = 0;
				}
				else if (j >= 1)//考虑边界条件
				{
					if (obstacleGrid[i][j] == 0)
						dp[i][j] = dp[i][j - 1];
					else
						dp[i][j] = 0;
				}
			}
		}
		return dp[m - 1][n - 1];
	}
};


JW01二氧化碳传感器实战:5分钟搞定Arduino环境监测系统(附完整代码) 本文详细介绍了如何使用JW01二氧化碳传感器与Arduino快速搭建环境监测系统。通过简单的硬件连接和完整的代码示例,读者可以在5分钟内实现CO2浓度实时监测,并了解数据解析、常见问题解决及进阶应用扩展。JW01传感器的高精度和易用性使其成为创客和嵌入式开发者的理想选择。 阅读详情

相关推荐

leetcode63-LeetCode-Cpp:C++版本的LeetCode算法

leetcode63 LeetCode-Cpp array topic 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.图片(二维矩阵)顺时针90度翻转 12. 【hard,找到数组中缺失的第一个正数】 13. 【hard,接雨水】 14. 【LeetCode54 顺时针打印二维矩阵】 15. 【LeetCode55 一维数组,跳跃游戏】 16. 【LeetCode59 顺时针打印螺旋矩阵方阵】 sort topic 1. 2. 3. 4. 5. 6. 7. 8. 9. binary-search topic 1. 【有序数据中,查找等于target的index左右界】 2. 【有序数组查找目标值的index。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。】 3. 【找到数组中重复的数】 tree topic 前序、中序、后序的Morris遍历方法: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 【二叉树的右视结果】 string topic 1. 2. 3. 【按z字形排列的字符串,输出按行读取的字符串】 4. 【字符串转成in

leetcode做题笔记63

本题对动态规划进一步考察,将有障碍物的情况考虑进去即可解决。

si_mple_的博客 333

STM32H7双核架构下的FreeRTOS实时调度算法优化实践.pdf

该文档【STM32H7双核架构下的FreeRTOS实时调度算法优化实践】共计 35 页,文档支持目录章节跳转同时还支持阅读器左侧大纲显示和章节快速定位,文档内容完整、条理清晰。文档内所有文字、图表、目录等元素均显示正常,无任何异常情况,敬请您放心查阅与使用。文档仅供学习参考,请勿用作商业用途。在当今科技发展的大背景下,嵌入式物联网虽不似一些前沿科技那般耀眼夺目,却在默默推动着各领域的进步。它将嵌入式系统与互联网紧密相连,让备具备联网通信能力。在智能家居中,它使家电远程可控,生活更便捷;在工业生产里,能实现备的远程监控与管理,提升效率。其技术成熟稳定,能在复杂环境中可靠运行。对于开发者而言,它提供了广阔的创新空间;对于企业来说,可助力产品智能化升级。嵌入式互联网,虽低调前行,却以其独特价值,悄然改变着我们的生活与生产方式。

leetcode每天一题-动态规划-63不同路径2

和上一道不同路径1很相似,只不过多了一个障碍,我们只需把有障碍的位置的路径数置为0即可,其他不变。

weixin_51406400的博客 335

LeetCode_63

LeetCode_63题意以及限制条件想到的所有可能解法对应的代码测试样例 题意以及限制条件 题目: 限制条件: 想到的所有可能解法 Ways_1——DP1(顺推) 时间复杂度——O(mn);空间复杂度——O(n)。 Ways_2——DP2(逆推) 时间复杂度——O(mn);空间复杂度——O(n)。 Ways_3——DP3(顺推) 时间复杂度——O(mn);空间复杂度——O(mn)。 Ways_4——DP4(逆推) 时间复杂度——O(mn);空间复杂度——O(mn

jacklovemonica的博客 281

leetcode----63. Unique Paths II

链接: https://leetcode.com/problems/unique-paths-ii/ 大意: 给定一个二维整数矩阵,矩阵中的元素为0/1。1表示该位置是障碍位置。求出从起点(左上角)到达终点(右下角)有多少种方案?注意:无法越过障碍位置。例子: 思路: 和上一题思路一样:https://blog.csdn.net/smart_ferry/article/details...

smart_ferry的博客 232

LeetCode63题 - 不同路径 II

LeetCode63题 - 不同路径 II

流水账 828

LeetCode | 63. Unique Paths II

63. Unique Paths II Description 描述:https://leetcode.com/problems/unique-paths-ii/description/ 题意:m列 × n行 的有障碍物的网格,从左上角第一个到右下角最后一个(只能往右和下这两个方向走),有多少条不重复的路径 Solution: (Java) class Solution { public ...

Karmen199x的博客 202

Leetcode 63. Unique Paths II 不同路径 II

Leetcode 63. Unique Paths II 不同路径 II 标签: Leetcode 题目地址: https://leetcode-cn.com/problems/unique-paths-ii/ 题目描述 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Fi...

CoderWangSon 342

LeetCode 63Unique Paths II(动态规划)

题目来源:https://leetcode.com/problems/unique-paths-ii/ 问题描述 63.Unique Paths II Medium A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below). The robot can ...

da_kao_la的博客 518

LeetCode】解题63Unique Paths II

LeetCode解题 63Unique Paths II Problem 63: Unique Paths II [Medium]解题思路Solution (Java) Problem 63: Unique Paths II [Medium] A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in ...

Fayedily的博客 275

LeetCode刷题:62. Unique Paths63. Unique Paths II

LeetCode刷题:62. Unique Paths Unique Paths 原题链接:https://leetcode.com/problems/unique-paths/description/ Unique Paths II 原题链接:https://leetcode.com/problems/unique-paths-ii/description/ 62. Unique P...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 442

LeetCode算法题之63. Unique Paths II(Medium)【Python3题解】

题目描述: A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in t

DaMoWangZQ的博客 327

python写算法题:leetcode: 63. Unique Paths II

class Solution(object): def __init__(self): self.cnt={} self.cnt[(0,0)]=1 def uniquePathsWithObstacles(self, obstacleGrid): """ :type obstacleGrid: List[Lis...

176

Leetcode63. 不同的路径 IIUnique Paths II

Leetcode - 63 Unique Paths II (Medium) 题目描述:和上一道题类似,只不过在路径中添加了障碍物,给定路径数组,1 表示障碍物,0 表示正常方块。 Input: [ [0,0,0], [0,1,0], [0,0,0] ] Output: 2 Explanation: There is one obstacle in the middle of the ...

厉兵秣码 203

Leetcode-62. Unique Paths and 63. Unique Paths II

Leetcode-62. Unique Paths 题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in tim

caoyan_12727的博客 507

[leetcode][python]63. Unique Paths II

63. Unique Paths II 知识点:dynamic programming 1. 原题 A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any ...

qq_40670635的博客 332

leetcode】【63Unique 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 respective

斗code、年华 329

leetcode.array--63. Unique Paths II

题目:63. Unique Paths II 题目链接:https://leetcode.com/problems/unique-paths-ii/description/ 给定一个二维矩阵,起点在左上方,终点在右下方,0代表可以通过,1代表障碍无法通过,每次只能向下或者向右移动一步,计算从起点到终点一共有多少种不同的走法? 也是一个简单的动态规划吧,位置grid[i][j]只能从grid[

Nero Zhang的博客 319

[Lintcode]115. Unique Paths II/[Leetcode]63. Unique Paths II

#115. Unique Paths II/63. Unique Paths II 本题难度: Easy/Medium Topic: Dynamic Programming Description Follow up for “Unique Paths”: Now consider if some obstacles are added to the grids. How many uniqu...

siriusli1981的博客 218

leetcode 62. Unique Paths63. Unique Paths II

62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { if(m <= 0 || n <= 0) return 0; vector<vector<int> > dp(m,vector<i...

weixin_30895603的博客 91
上一篇: LeetCode62——Unique Paths
下一篇: LeetCode64——Minimum Path Sum
NearXDU
博客等级 码龄12年 738粉丝 587原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值