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.
===============
思路:经典DP动态规划入门问题,
定义状态转移方程f[i][j] = min(f[i-1][j],f[i][j-1])+grid[i][j]
初始状态函数,二维数组f[][]的第一行和第一列
========
code 如下:
class Solution{
public:
int minPathSum(vector<vector<int> > &grid){
if(grid.size()==) return ;
const int m = grid.size();
const int n = grid[].size();
int f[m][n];
f[][] = grid[][];
for(int i = ;i<m;i++){
//初始化状态方程第一lie
f[i][] = f[i-][]+grid[i][];
}
for(int i = ;i<n;i++){
//初始化状态方程第一hang
f[][i] = f[][i-]+grid[][i];
}
//运用状态方程求解
for(int i = ;i<m;i++){
for(int j = ;j<n;j++){
if(f[i-][j]<f[i][j-]){
cout<<i-<<"-"<<j<<endl;
}else{
cout<<i<<"-"<<j-<<endl;
}
f[i][j] = min(f[i-][j],f[i][j-])+grid[i][j];
}
}
return f[m-][n-];
}
};
64. Minimum Path Sum的更多相关文章
- leecode 每日解题思路 64 Minimum Path Sum
题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...
- 刷题64. Minimum Path Sum
一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...
- 【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 最小路径和
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
Problem: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri ...
- C#解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 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 ...
- 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 ...
- 64. Minimum Path Sum(中等, 又做出一个DP题, 你们非问我开不开心,当然开心喽!^^)
Given an m x n grid filled with nonnegative numbers, find a path from top left to bottom right which ...
随机推荐
- linux笔记_文件搜索命令
一.locate命令 locate命令属于mlocate包,如果执行locate filename提示命令未找到执行安装mlocate包 # yum -y install mlocate 安装后执行l ...
- NOIP2011 普及組 統計單詞數
题目描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数. 现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给 ...
- HDU 2072 单词数
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2072 普通解法: /* HDU 2072 单词数 --- 字符串处理 */ #include < ...
- 20145220&20145209&20145309信息安全系统设计基础实验报告(1)
实验贡献:韩旭飞 刘一阳 李昊实验报告贡献:李昊 刘一阳 韩旭飞 PART1 一.实验原理 交叉编译,简单地说,就是在一个平台上生成另一个平台上的可执行代码.同一个体系结构可以运行不同的操作系统:同样 ...
- 多线程问题(JVM重排序)
public class Test3 { private static boolean ready; private static int Number; private static class R ...
- 关于MSP430中断机制
中断很大程度上体现了一款单片机的性能,从这一点将MSP430在中断方面做得很不错,主要是提供了非常丰富的中断源,基本的有IO中断,定时器中断和一些接口中断(SPI,UART,I2C)等等. 现 ...
- Compiler ,Interpreter, Linker
https://en.wikipedia.org/wiki/Interpreter_(computing) https://en.wikipedia.org/wiki/Compiler https:/ ...
- Articles Every Programmer Must Read
http://javarevisited.blogspot.sg/2014/05/10-articles-every-programmer-must-read.html Being a Java pr ...
- 关于margin和padding的总结
总结一下: 要想实现如(图一)效果,(即一个div中的子元素与父元素有间距): 如果类名为.middle的父元素没有写border,则类名为firstChild的子元素设置margin-top,会导致 ...
- 最大化 AIX 上的 Java 性能,第 4 部分: 监视流量
http://www.ibm.com/developerworks/cn/aix/library/es-Javaperf/es-Javaperf4.html 最大化 AIX 上的 Java 性能,第 ...