Leetcode64.Minimum Path Sum最小路径和
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
说明:每次只能向下或者向右移动一步。
示例:
输入: [ [1,3,1], [1,5,1], [4,2,1] ] 输出: 7 解释: 因为路径 1→3→1→1→1 的总和最小。
这题因为限制了只能往下或者往右走一步,所以可以从0开始从上往下遍历,其他类型的题或许就要从最大的开始遍历等等。
class Solution {
public:
int minPathSum(vector<vector<int> >& grid) {
int r = grid.size();
int c = grid[0].size();
vector<vector<int> > dp(r, vector<int>(c, 0));
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
if(i == 0 && j == 0)
dp[0][0] = grid[0][0];
else if(i == 0)
{
dp[i][j] = dp[i][j - 1] + grid[i][j];
}
else if(j == 0)
{
dp[i][j] = dp[i - 1][j] + grid[i][j];
}
else
{
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
}
}
}
return dp[r - 1][c - 1];
}
};
Leetcode64.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]]根据 ...
- 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 ...
- leetcode64. Minimum Path Sum
这个题是从左上角到右下角的路径和最小,实际就是一道dp题. 第一种写法是只初始化(0,0)位置,第二种写法则是把第一行.第一列都初始化了.个人更喜欢第二种写法,简单一点. dp的右下角的值就为最终的值 ...
- 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 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...
随机推荐
- js获取网页屏高 屏宽
<SCRIPT LANGUAGE="JavaScript"> <!-- //document.body.scrollTop 滚动条的上端距离 //document ...
- git difff
Generate patch through git diff http://stackoverflow.com/questions/1191282/how-to-see-the-changes-be ...
- hession RMI 远程调用
/** * * @author administror * 在java中,需要去extends 继承java.rmi.Remote 接口,才能称为在于服务器流的远程对象. * 各客服端调用 * */p ...
- WhaleCTF之隐写-Find
WhaleCTF之隐写-Find 前往题目 图片保存到本地,用Stegsolve打开图片 找到二维码 用微信或qq扫描,得到flag~
- 深度神经网络Google Inception Net-V3结构图
深度神经网络Google Inception Net-V3结构图 前言 Google Inception Net在2014年的 ImageNet Large Scale Visual Recognit ...
- 05.Hibernate常用的接口和类---Configuration类和作用
Configuration作用: 加载Hibernate配置文件,可以获取SessionFactory对象 加载方式: 1.加载配置文件 Configuration configuration = n ...
- 关于js的闭包和复制对象
一.有关js的闭包 1.概念:所谓的闭包,就是指的两个作用域,其中内层作用于可以访问外层作用域的函数的现象 2.简单应用 for(var i = 0;i< lis.lenth;i++){ (fu ...
- 提示框插件layer的使用讲解
官方网站:http://layer.layui.com/ 在页面中引入: <script src="js/layer-v3.0.3/layer/layer.js">&l ...
- python基础---内置函数 和 匿名函数 知识点自查填空题
1.file ---默认是输出到(),如果设置为(),输出到() 2.sep---打印(),默认为() 3.end---每一次打印的结尾,默认为() 4.flush---立即把内容输出到(),不做() ...
- alert提示框去掉域名
window.alert = function(name){ var iframe = document.createElement("IFRAME"); iframe.style ...