当前位置:网站首页>Topic37——64. 最小路径和
Topic37——64. 最小路径和
2022-06-27 11:41:00 【_卷心菜_】
题目:给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
说明:每次只能向下或者向右移动一步。
示例 1:

输入:grid = [[1,3,1],[1,5,1],[4,2,1]]
输出:7
解释:因为路径 1→3→1→1→1 的总和最小。
示例 2:
输入:grid = [[1,2,3],[4,5,6]]
输出:12
提示:
m == grid.length
n == grid[i].length
1 <= m, n <= 200
0 <= grid[i][j] <= 100
双重循环:
class Solution {
public int minPathSum(int[][] grid) {
for(int i = 0; i < grid.length; i++) {
for(int j = 0; j < grid[0].length; j++) {
int temp = Integer.MAX_VALUE;
if(j-1 >= 0)
temp = Math.min(temp, grid[i][j-1]);
if(i-1 >= 0)
temp = Math.min(temp, grid[i-1][j]);
if(i-1 < 0 && j-1 < 0)
temp = 0;
grid[i][j] = grid[i][j] + temp;
}
}
return grid[grid.length-1][grid[0].length-1];
}
}
官方解法(对上述方法的改进):
class Solution {
public int minPathSum(int[][] grid) {
int[][] dp = new int[grid.length][grid[0].length];
dp[0][0] = grid[0][0];
for(int i = 1; i < grid.length; i++) {
dp[i][0] = dp[i-1][0] + grid[i][0];
}
for(int j = 1; j < grid[0].length; j++) {
dp[0][j] = dp[0][j-1] + grid[0][j];
}
for(int i = 1; i < grid.length; i++) {
for(int j = 1; j < grid[0].length; j++) {
dp[i][j] = grid[i][j] + Math.min(dp[i-1][j], dp[i][j-1]);
}
}
return dp[grid.length-1][grid[0].length-1];
}
}
边栏推荐
- 杰理之添加定时器中断【篇】
- R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用VGAM包的vglm函数对有序多分类logistic回归模型进行平行性假设作检验
- R language uses the poisgof function of epidisplay package to test the goodness of fit of Poisson regression and whether there is overdispersion
- How to modify a node_ Files in modules
- In depth analysis of error solutions and problems in dynamic loading of unity shadow and outline components
- The DBSCAN function of FPC package in R language performs density clustering analysis on data, and the plot function visualizes the clustering graph
- AUTOCAD——三种修剪方式
- pull request
- 进程间通信详解
- Qstype implementation of self drawing interface project practice (I)
猜你喜欢

Excel中输入整数却总是显示小数,如何调整?

JSP自定义标签

Summary of qstype class usage (II)

57. The core principle of flutter - layout process

StarCraft's Bug King ia retired for 2 years to engage in AI, and lamented that it was inferior

QStyle类用法总结(二)

56. Core principle of flutter - flutter startup process and rendering pipeline

【On nacos】快速上手 Nacos

Prevent being rectified after 00? I. The company's recruitment requires that employees cannot sue the company

Jwas: a Bayesian based GWAS and GS software developed by Julia
随机推荐
杰理之IO 口中断使用注意事项【篇】
R language uses the poisgof function of epidisplay package to test the goodness of fit of Poisson regression and whether there is overdispersion
R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, uses step function to realize stepwise re
Don't miss it. New media operates 15 treasure official account to share
dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article
怎么找相同台词的影视片段?这8个电影搜索神器,一句台词找到对应片段
建木持续集成平台v2.5.0发布
AUTOCAD——三种修剪方式
Unity Shader学习(二)第一个Shader
Thinkphp6 interface limits user access frequency
手把手带你入门 API 开发
巅峰小店APP仿站开发玩法模式讲解源码分享
【On nacos】快速上手 Nacos
Safe landing practice of software supply chain under salesforce containerized ISV scenario
最大路径和问题(摘樱桃问题)
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布、使用dot.col参数指定分组数据点的颜色
面试突击60:什么情况会导致 MySQL 索引失效?
Interview shock 60: what will cause MySQL index invalidation?
Comment modifier Node Fichiers dans les modules
The wonderful use of 0 length array in C language