36.Minimum Path Sum(最小路径和)
Level:
Medium
题目描述:
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.
思路分析:
题目要求找出从矩阵的左上角到右下角最短的路径长度,我们可以采用动态规划的思想求解,我们用dp[ i ] [ j ]来表示走到第i行和第j列的最短路径长度。由题意知,机器人只能向右和向下走,那么状态转移方程是 dp[ i ] [ j ]=min(dp [i-1] [ j ],dp[ i ] [ j-1])+grid[ i ] [ j ]。注意到矩阵第一行或者第一列某个位置,路径只有一条。(因为起点是左上角,并且只能向右向下移动)
代码:
public class Solution{
public int minPathSum(int [][]grid){
if(grid==null||grid.length==0)
return 0;
int [][]dp=new int [grid.length][grid[0].length];
int i,j;
dp[0][0]=grid[0][0];
for(i=1;i<grid.length;i++){
dp[i][0]=dp[i-1][0]+grid[i][0];
}
for(j=1;j<grid[0].length;j++){
dp[0][j]=dp[0][j-1]+grid[0][j];
}
for(i=1;i<grid.length;i++){
for(j=1;j<grid[0].length;j++){
dp[i][j]=Math.min(dp[i-1][j],dp[i][j-1])+grid[i][j];
}
}
return dp[i-1][j-1];
}
}
36.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 ...
- 064 Minimum Path Sum 最小路径和
给定一个只含非负整数的 m x n 网格,找到一条从左上角到右下角的可以使数字之和最小的路径.注意: 每次只能向下或者向右移动一步.示例 1:[[1,3,1], [1,5,1], [4,2,1]]根据 ...
- 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 ...
- Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)
Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...
- LeetCode 64. 最小路径和(Minimum Path Sum) 20
64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...
随机推荐
- JavaScript中的反柯里化
转载自:https://www.cnblogs.com/zztt/p/4152147.html 柯里化 柯里化又称部分求值,其含义是给函数分步传递参数,每次传递参数后部分应用参数,并返回一个更具体的函 ...
- vue下超级滚动条perfect-scrollbar(在特定框架里使用一款并非为该框架定制的库/插件)
点我查看
- linux篇—Nginx反向代理负载均衡
一.环境准备 反向代理功能架构 3台web服务器,组建出web服务器集群 web01 10.0.0.7 172.16.1.7 web02 10.0.0.8 172.16.1.8 web03 10.0. ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
- quickbi报错AE0510000002
数据库连接异常: com.alipay.dwsap.common.exception.AlisisRuntimeException: 7 AE0510000002:Database access er ...
- python基础:7.求结果
求结果: v1 = 1 or 3 v2 = 1 and 3 v3 = 0 and 2 and 1 v4 = 0 and 2 or 1 v5 = 0 and 2 or 1 or 4 v6 = 0 o ...
- 【leetcode】1012. Complement of Base 10 Integer
题目如下: Every non-negative integer N has a binary representation. For example, 5 can be represented a ...
- 微信小程序中显示html富文本的方法
微信小程序中显示html富文本的方法 使用方法:git地址:https://github.com/icindy/wxParse 一.下载wxParse文件 二.在要引入的页面的js文件中,引入文件 j ...
- vue项目中使用echarts地图
第一步.npm install echarts 第二部.在main.js中引入 第三步.创建组件,并且用this.$echarts.init初始化echarts <template> &l ...
- .Net Core 使用 Swagger 提供API文档
1.运行环境 开发工具:Visual Studio 2017 JDK版本:.NET Core 2.0 项目管理工具:nuget 2.GITHUB地址 https://github.com/nbfujx ...