LeetCode OJ 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 minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
【题目分析】
一个m x n的格子,每一个格子上是一个非负整数,找到从左上角到右下角的路线,它所经过的格子的值的和最小,返回这个最小值。要求只能每一步只能向右或者向下移动。
【思路】
在前面的几个题目的基础上我们不难想出解决办法。
1. 对于第一行,A[0][j] += A[0][j-1];
2. 对于第一列,A[i][0] += A[i-1][0];
3. 对于其他值,A[i][j] += min(A[i-1][j],A[i][j-1]);
---->
---->

通过这几个图我们可以看到求解的过程。
【java代码】
public class Solution {
public int minPathSum(int[][] grid) {
int row = grid.length;
int col = grid[0].length;
if(row == 0 || col == 0) return 0;
int[] dp = new int[col];
dp[0] = grid[0][0];
for(int j = 1; j < col; j++)
dp[j] = grid[0][j] + dp[j-1];
for (int i = 1; i < row; i++){
dp[0] += grid[i][0];
for (int j = 1; j < col; j++){
dp[j] = Math.min(dp[j-1], dp[j]) + grid[i][j];
}
}
return dp[col - 1];
}
}
LeetCode OJ 64. Minimum Path Sum的更多相关文章
- 【LeetCode】64. Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- 【一天一道LeetCode】#64. Minimum Path Sum.md
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】64. Minimum Path Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode OJ: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 DP]64. Minimum Path Sum
一个m*n的表格,每个格子有一个非负数,求从左上到右下最短的路径值 和62,63两个值是同一个思路,建立dp表,记录每个位置到右下角的最短路径的值 class Solution(object): de ...
- leecode 每日解题思路 64 Minimum Path Sum
题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...
- 刷题64. Minimum Path Sum
一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...
- 【LeetCode练习题】Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- [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 ...
随机推荐
- unity3d和php后台简单交互--二
上次我们讨论了u3d和php的简单交互,现在我们接着讨论u3d和php交互,这里我们讨论的是php的后台大家可以延伸为其他语言.在实现的开发中我们很少通过发送字符或者字段到服务器上的,我们一般会请求包 ...
- angularjs-xeditable整合typeahead完成智能提示
按照需求,需要在angularjs的xeditable中加入typeahead,来完成智能提示,并且在选择后,把该条数据的其他信息也显示在此行中,于是做了一下的测试修改. 当然,既然用了xeditab ...
- Jmeter(四)-断言/检查点
断言就类似LoadRunner中的检查点.对上一个请求返回的信息,做字符串.数据包大小.HTML.XML.图片等做判断,确保返回的信息的准确性. 添加响应断言:欢迎您 如果登陆页登陆成功,则后台会返回 ...
- MySQL数据库安装(CentOS操作系统/tar.gz方式)
1. 上传Mysql安装包“mysql-5.5.40-linux2.6-x86_64.tar.gz”到部署机,位置任意: 2. 将Mysql安装包解压到其所在目录,命令如下: -linux2.-x86 ...
- extjs 6.2 helloworld
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出
这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999 Problem Description: As we know,the sha ...
- Git 的版本库创建和修改
什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或 ...
- portal安装常见问题
1.创建portaladmin的url? https://portal.smart.ningbo:7443/arcgis/home/createadmin.html 2.创建portaladmin一闪 ...
- 解决asp.net中“从客户端中检测到有潜在危险的Request.Form值”的错误
修改Web.config,增加requestValidationMode="2.0"属性值 <system.web> <httpRuntime requestVa ...
- nginx 403
location / { autoindex on; } chown -R www-data:www-data /var/www usermod -a -G www-data rootnano /et ...