给定一个只含非负整数的 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 最小路径和的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. Leetcode64.Minimum Path Sum最小路径和

    给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [   [1,3,1], [1,5,1] ...

  5. minimun path sum(最小路径和)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  6. 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 ...

  7. 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 ...

  8. 【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 ...

  9. Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)

    Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...

随机推荐

  1. ***静态成员的定义及初始化 for c++ for新用法

    静态成员的初始化要在类外不然报错error: ISO C++ forbids in-class initialization of non-const static member '***' 但是声明 ...

  2. BZOJ_1304_[CQOI2009]叶子的染色_树形DP

    BZOJ_1304_[CQOI2009]叶子的染色_树形DP Description 给一棵m个结点的无根树,你可以选择一个度数大于1的结点作为根,然后给一些结点(根.内部结点和叶子均可)着以黑色或白 ...

  3. BZOJ_1998_[Hnoi2010]Fsk物品调度_并查集+置换

    BZOJ_1998_[Hnoi2010]Fsk物品调度_并查集+置换 Description 现在找工作不容易,Lostmonkey费了好大劲才得到fsk公司基层流水线操作员的职位.流水线上有n个位置 ...

  4. 【LeetCode】259 3Sum Smaller

    题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...

  5. MySQL 5.7新特性

    新增特性 Security improvements. MySQL.user表新增plugin列,且若某账户该字段值为空则账户不能使用.从低版本MySQL升级至MySQL5.7时要注意该问题,且建议D ...

  6. 学习Tomcat动态加载JSP的Class类

    今天在修改项目一个JSP文件时,突然想到Tomat是怎么实现动态实时加载JSP编译后的class类的? 查了半天资料,看了很多文章,终于明白是怎么回事了:ClassLoader,当tomcat发现js ...

  7. 一 Kubernetes介绍

    Kubenetes是一款由Google开发的开源的容器编排工具,它可以解决以下分布式环境下的问题: 调度 你已经得到了这个很棒的基于容器的应用程序? 太棒了!现在你需要确保它能够运行在它应该运行的地方 ...

  8. warning: conflicting types for built-in function 'puts'

    warning: conflicting types for built-in function 'puts' [编译器版本] arm-linux-gcc 3.4.1 [问题描述] 在做嵌入式底层开发 ...

  9. Balloon Comes! hdu(小数位数处理)

    Balloon Comes! Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): A ...

  10. nodejs写文件

    var fs = require('fs'); 2 var txt = "以上程序使用fs.readFileSync从源路径读取文件内容,并使用fs.writeFileSync将文件内容写入 ...