064 Minimum Path Sum 最小路径和
给定一个只含非负整数的 m x n 网格,找到一条从左上角到右下角的可以使数字之和最小的路径。
注意: 每次只能向下或者向右移动一步。
示例 1:
[[1,3,1],
[1,5,1],
[4,2,1]]
根据上面的数组,返回 7. 因为路径 1→3→1→1→1 总和最小。
详见:https://leetcode.com/problems/minimum-path-sum/description/
Java实现:先处理最左边和最上边两条边,因为只有一条路。接下来每一点的值等于它上边和左边的较小值加上该点的数值~即为到达该点的最短路径。
class Solution {
public int minPathSum(int[][] grid) {
int m=grid.length;
int n=grid[0].length;
int[][] dp=new int[m][n];
dp[0][0]=grid[0][0];
for(int j=1;j<n;++j){
dp[0][j]=dp[0][j-1]+grid[0][j];
}
for(int i=1;i<m;++i){
dp[i][0]=dp[i-1][0]+grid[i][0];
}
for(int i=1;i<m;++i){
for(int j=1;j<n;++j){
dp[i][j]=Math.min(dp[i-1][j],dp[i][j-1])+grid[i][j];
}
}
return dp[m-1][n-1];
}
}
C++实现:
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m=grid.size();
int n=grid[0].size();
int dp[m][n];
dp[0][0]=grid[0][0];
for(int i=1;i<m;++i)
{
dp[i][0]=grid[i][0]+dp[i-1][0];
}
for(int j=1;j<n;++j)
{
dp[0][j]=grid[0][j]+dp[0][j-1];
}
for(int i=1;i<m;++i)
{
for(int j=1;j<n;++j)
{
dp[i][j]=grid[i][j]+min(dp[i-1][j],dp[i][j-1]);
}
}
return dp[m-1][n-1];
}
};
参考:https://www.cnblogs.com/grandyang/p/4353255.html
064 Minimum Path Sum 最小路径和的更多相关文章
- [LeetCode] Minimum Path Sum 最小路径和
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- [LeetCode] 64. Minimum Path Sum 最小路径和
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- [leetcode]64. Minimum Path Sum最小路径和
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Leetcode64.Minimum Path Sum最小路径和
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [ [1,3,1], [1,5,1] ...
- minimun path sum(最小路径和)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 64. Minimum Path Sum(最小走棋盘 动态规划)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Java for LeetCode 064 Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 【LeetCode】064. Minimum Path Sum
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
- Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)
Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...
随机推荐
- smack api 转载未测试
===============================================================主动发送信息给某个用户-------------------------- ...
- ajax异步上传文件FormDate方式,html支持才可使用
今天需要做一个头像的预览功能,所以我想到了异步上传文件. 总结几点: 异步上传难点: 文件二进制流如何获取 是否需要设置表单的头,就是content-Type那里.异步,所以无所谓了吧. 其他就差不多 ...
- linux命令学习笔记(46):vmstat命令
vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动 进行监控.他是对系统的整体情况进行统计,不足之处是无法对某个进程进行 ...
- The Django Book 2.0--中文版
Table of contents 2.0, English -> Chinese 第一章:介紹Django阅读 01 第二章 入门阅读 02 第三章 视图和URL配置阅读 03 第四章:模版阅 ...
- jdk安装图解--windows系统(第一次安装和第二次安装区别)
第一次安装可参考 https://jingyan.baidu.com/article/22fe7cedc9b93e3003617f64.html 第二次安装,如已经配置好环境变量,cmd下执行java ...
- Python3解leetcode Single Number
问题描述: Given a non-empty array of integers, every element appears twice except for one. Find that sin ...
- MySQL 学习三 关于转义
DB2 LIKE谓词查询语句中支持 百分号(%).下划线(_)的使用,不支持方括号([])(注:它会把方括号当成实际的值而非通配符),当我们需要在LIKE 查询条件中将百分号(%).下划线(_)作为实 ...
- ie8兼容rgba的方法
现在做个网页还得考虑ie8,只想说:尼玛! 但是没办法,屈于淫威也得弄. 首先说下rgba的含义吧,rgba,r代表red,g代表green,b代表blue,a代表透明度. filter:progid ...
- 清除@SessionAttributes 网站实现退出登录
在网站实现登录时,我认识了@SessionAttributes,对我来说是真的好用,@SessionAttributes注解可以使得模型中的数据存储一份到session域中. 这样在页面跳转时可以直接 ...
- iis部署错误:HTTP 错误 500.21 - Internal Server Error
将网站发布到IIS,访问发生如下错误: HTTP 错误 500.21 - Internal Server Error处理程序“PageHandlerFactory-Integr”在其模块列表中有一个错 ...