当前位置:网站首页>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];
}
}
边栏推荐
- 聊聊 Go 语言与云原生技术
- 0基础了解电商系统如何对接支付渠道
- Jerry's DAC output mode setting [chapter]
- . Net6 access skywalking link tracking complete process
- R语言dplyr包arrange函数排序dataframe数据、通过多个数据列排序dataframe数据、指定第一个字段降序排序,第二字段不指定(默认升序排序)
- Research Report on the overall scale, major manufacturers, major regions, products and application segments of hydraulic torque in the global market in 2022
- C # WPF realizes undo redo function
- Comment modifier Node Fichiers dans les modules
- master公式
- Daily leetcode force deduction (21~25)
猜你喜欢
nifi从入门到实战(保姆级教程)——身份认证
Unity shader learning (II) the first shader
I.MX6ULL启动方式
Youboxun attended the openharmony technology day to create a new generation of secure payment terminals
器审科普:创新医疗器械系列科普——胸骨板产品
What is the TCP 3-time handshake process?
[tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction
Research Report on the overall scale, major manufacturers, major regions, products and application segments of hydraulic torque in the global market in 2022
In 2021, the global enhanced oil production surfactant revenue was about USD 202.3 million, and it is expected to reach USD 297.1 million in 2028
Usage of rxjs mergemap
随机推荐
[tcapulusdb knowledge base] tcapulusdb system user group introduction
How to modify a node_ Files in modules
Challenges of machine learning system in production
等等, 怎么使用 SetMemoryLimit?
Research Report on the overall scale, major manufacturers, major regions, products and application segments of hydraulic torque in the global market in 2022
Unity Shader学习(一)认识unity shader基本结构
"24 of the 29 students in the class successfully went to graduate school" rushed to the hot search! Where are the remaining five?
Informatics Olympiad all in one 1332: [example 2-1] weekend dance
Redis distributed lock 15 ask, what have you mastered?
面试突击60:什么情况会导致 MySQL 索引失效?
Jerry's DAC output mode setting [chapter]
Wechat applet realizes five-star evaluation
0 basic understanding of how e-commerce systems connect with payment channels
R language dplyr package arrange function sorts dataframe data, sorts dataframe data through multiple data columns, specifies the first field to be sorted in descending order, and does not specify the
Xuri 3sdb, installing the original ROS
Precautions for use of IO interface interrupt of Jerry [chapter]
In 2021, the global carbon graphite brush revenue is about US $2366million, and it is expected to reach US $2701.8 million in 2028
MapReduce principle analysis (in-depth source code)
优博讯出席OpenHarmony技术日,全新打造下一代安全支付终端
Leetcode 177 The nth highest salary (June 26, 2022)