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 ...
随机推荐
- linux下使用autoconf制作Makefile
第一步:常用工具安装:正所谓:"工欲善其事,必先利其器".我们常用的工具主要有GNU AutomakeGNU AutoconfGNU m4GNU Libtool1.查看自己系统中是 ...
- 2016-2017 CT S03E02: Codeforces Trainings Season 3 Episode 2
A HHPaint B Square Root C Interesting Places D Road to Home E Ant and apples F Square G Pair H The F ...
- Java错误提示is not an enclosing class
今天脑袋晕乎乎的,犯了个低级错误,好半天才反应过来 一直提示:is not an enclosing class 我居然把 RegisterActivity.class 写成了 RegisterAct ...
- Mediawiki随笔
http://www.ibm.com/developerworks/cn/opensource/os-mediawiki/ 定制wiki http://www.zzbaike.com/wiki/Me ...
- css2和CSS3的background属性简写
1.css2:background:background-color || url("") || no-repeat || scroll || 0 0; css3: backg ...
- React native android 最常见的10个问题
这里逐条记录下最容易遇到的React native android 相关case: 1. app启动后,红色界面,unable load jsbundle : 解决办法:一般来说就是,你是用dev-s ...
- IE9 浏览器无法捕获中键事件
在silverlight中由于需要添加中键事件,所以通过hook来捕获,但是在IE9上无法运行,该问题是由于IE9设置问题,如下图修改配置, 取消选择即可:
- Openjudge-NOI题库-Pell数列
题目描述 Description Pell数列a1, a2, a3, ...的定义是这样的,a1 = 1, a2 = 2, ... , an = 2 * an − 1 + an - 2 (n > ...
- Hbulider里面template模板自用
template.js 一款 JavaScript 模板引擎,简单,好用.提供一套模板语法,用户可以写一个模板区块,每次根据传入的数据,生成对应数据产生的HTML片段,渲染不同的效果. 特性: 模版编 ...
- ionic 进入多级目录以后隐藏底部导航栏(tabs)(完美解决方案)
公司开始使用ionic开发项目,在此记录下把遇到的问题,网上有大牛已经把解决方法整出来了,不过记录在自己这里方便查阅. 这篇记录在有tabs的项目里,进入子层级时,底部导航还一直存在,本人是要让他只在 ...