当前位置:网站首页>剑指 Offer II 098. 路径的数目 / 剑指 Offer II 099. 最小路径之和
剑指 Offer II 098. 路径的数目 / 剑指 Offer II 099. 最小路径之和
2022-06-26 20:32:00 【彼淇梁】
剑指 Offer II 098. 路径的数目【中等题】
思路:【动态规划】
二阶dp数组:dp[i][j]表示从左上角走到下标为(i,j)的位置所需要的路径数目。
边界条件:dp[0][0]=1
i = 0时:
终点位于矩阵上边界,dp[i][j]的值仅取决于左侧点dp
即dp[0][j] = dp[0][j-1]
j = 0时:
终点位于矩阵左边界,dp[i][j]的值仅取决于上侧点dp
即dp[i][0] = dp[i-1][0]
其实左边界和上边界的点从左上角出发的话,仅有一种路径到达,
即dp[0][j] = 1,dp[i][0] = 1。
转移方程:
dp[i][j] = dp[i-1][j] + dp[i][j-1]
代码:
class Solution {
public int uniquePaths(int m, int n) {
//动态规划
//定义dp数组,表示走到(i,j)位置可选的路径数目
int[][] dp = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i > 0 && j > 0){
//转移方程
//走到(i,j)位置的路径数由走到它左侧和上侧的路径数共同决定
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}else {
//边界条件
// //1.左上角起点
// if (i == 0 && j == 0){
// dp[0][0] = 1;
// }else if (i == 0){//2.上边界
// dp[0][j] = dp[0][j-1];
// }else {//3.左边界
// dp[i][0] = dp[i-1][0];
// }
dp[i][j] = 1;
}
}
}
//(m-1,n-1)位置即为m×n网格的右下角,dp数组的路径数始终是从左上角开始出发计算的,因此dp[m-1][n-1]即为题目所求总路径数目
return dp[m-1][n-1];
}
}
剑指 Offer II 099. 最小路径之和【中等题】
思路:【动态规划】
解题思路参照上题,将dp[i][j]的值定义为从矩阵左上角走到(i,j)位置的最小路径和即可。
代码:
class Solution {
public int minPathSum(int[][] grid) {
//动态规划
//排除特殊输入
if (grid == null || grid.length == 0 || grid[0].length == 0){
return 0;
}
int m = grid.length,n = grid[0].length;
//定义数组dp dp[i][j]表示从矩阵左上角走到(i,j)时的最小路径和
int[][] dp = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i > 0 && j > 0){
//转移方程
dp[i][j] = Math.min(dp[i-1][j],dp[i][j-1]) + grid[i][j];
}else {
//边界条件
if (i == 0 && j == 0){
dp[0][0] = grid[0][0];
}else if (i == 0){
dp[0][j] = dp[0][j-1] + grid[0][j];
}else {
dp[i][0] = dp[i-1][0] + grid[i][0];
}
}
}
}
//dp[m-1][n-1]即为从矩阵左上角走到右下角的最小路径和
return dp[m-1][n-1];
}
}
边栏推荐
- Flutter TextField详解
- 好物推荐:移动端开发安全工具
- 西瓜书重温(七): 贝叶斯分类器(手推+代码demo)
- c语言简单的登录
- C: Reverse linked list
- 云计算技术的发展与芯片处理器的关系
- Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023
- Tiktok practice ~ search page ~ video details
- 【最详细】最新最全Redis面试大全(42道)
- IDEA 报错:Process terminated【已解决】
猜你喜欢

Tiktok practice ~ sharing module ~ generate short video QR code

leetcode刷题:字符串06(实现 strStr())

动态规划111

Super VRT

GameFi 活跃用户、交易量、融资额、新项目持续性下滑,Axie、StepN 能摆脱死亡螺旋吗?链游路在何方?

Establish a connection with MySQL

抖音实战~搜索页面~视频详情

分布式ID生成系统

Six necessary threat tracking tools for threat hunters
![[recommended collection] these 8 common missing value filling skills must be mastered](/img/ab/353f74ad73ca592a3f97ea478922d9.png)
[recommended collection] these 8 common missing value filling skills must be mastered
随机推荐
[Bayesian classification 3] semi naive Bayesian classifier
不要做巨婴了
好物推荐:移动端开发安全工具
【最详细】最新最全Redis面试大全(42道)
超分之VRT
Tiktok practice ~ sharing module ~ generate short video QR code
JS mobile terminal touch screen event
StringUtils判断字符串是否为空
Six necessary threat tracking tools for threat hunters
Solve com mysql. jdbc. exceptions. jdbc4.MySQLNonTransientConnectionException: Could not create connection
两个文件 合并为第三个文件 。
Development of NFT for digital collection platform
Button how to dynamically set drawablebottom (setcomposunddrawables is invalid)
抖音实战~搜索页面~视频详情
0 basic C language (3)
证券开户安全吗,有没有什么危险呢
Browser event loop
Developer survey: rust/postgresql is the most popular, and PHP salary is low
C# 练习。类列表加记录,显示记录和清空记录
leetcode刷题:字符串06(实现 strStr())