leetcode64. Minimum Path Sum
这个题是从左上角到右下角的路径和最小,实际就是一道dp题。
第一种写法是只初始化(0,0)位置,第二种写法则是把第一行、第一列都初始化了。个人更喜欢第二种写法,简单一点。
dp的右下角的值就为最终的值
第一种写法:
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int rows = grid.size();
if(rows <= )
return -;
int cols = grid[].size();
if(cols <= )
return -;
vector<vector<int> > result(rows,vector<int>(cols));
result[][] = grid[][];
for(int i = ;i < rows;i++){
for(int j = ;j < cols;j++){
if(i != && j != )
result[i][j] = grid[i][j] + min(result[i-][j],result[i][j-]);
if(i == && j != )
result[i][j] = result[i][j-] + grid[i][j];
if(j == && i != )
result[i][j] = result[i-][j] + grid[i][j];
}
}
return result[rows-][cols-];
}
};
第二种写法:
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m = grid.size();
if(m <= )
return ;
int n = grid[].size();
if(n <= )
return ;
vector<vector<int> > dp(m,vector<int>(n));
dp[][] = grid[][];
for(int i = ;i < m;i++)
dp[i][] = dp[i-][] + grid[i][];
for(int i = ;i < n;i++)
dp[][i] = dp[][i-] + grid[][i];
for(int i = ;i < m;i++){
for(int j = ;j < n;j++){
dp[i][j] = grid[i][j] + min(dp[i-][j],dp[i][j-]);
}
}
return dp[m-][n-];
}
};
leetcode64. Minimum Path Sum的更多相关文章
- Leetcode64.Minimum Path Sum最小路径和
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [ [1,3,1], [1,5,1] ...
- 【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 ...
- leecode 每日解题思路 64 Minimum Path Sum
题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...
- 【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之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
- 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] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- 【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 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
随机推荐
- 一:Bootstrap-css样式
页面大块布局: div.container 栅格系统: 一行分成 12 列 div.row div.col-md-12 div.col-xs-12 <div class="row&qu ...
- jsp技术知识点
1.jsp被Tomcat翻译成.java文件后,会被放在Tomcat安装目录下的\work\Catalina\localhost\station\org\apache\jsp文件夹下 2.El表达式表 ...
- [LeetCode]Combination Sum题解(DFS)
Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T), f ...
- PoPo数据可视化周刊第3期 - 台风可视化
9月台风席卷全球,本刊特别选取台风最佳可视化案例,数据可视化应用功力最深厚者,当属纽约时报,而传播效果最佳的是The Weather Channel关于Florence的视频预报,运用了数据可视化.可 ...
- Java中返回值的详解
package com.company; //java中main()函数中调用其他方法的两种方式//1.实例化对象 public class returnDemo { public static vo ...
- js-99乘法表的练习
<html> <head> <title>World</title> <style type="text/css"> & ...
- php 生成唯一id的几种解决方法(实例)
php 生成唯一id,网上查了下,有很多的方法 1.md5(time() . mt_rand(1,1000000)); 这种方法有一定的概率会出现重复 2.php内置函数uniqid() uniqid ...
- drupal7 STMP邮件模块配置
drupal7.54 STMP version = "7.x-1.6" 配置: 注意:上面的“用户名”需要和“站点信息”页面的电子邮件地址保持一致,邮件发送才能成功 ---- ...
- MapReduce两种执行环境介绍:本地测试环境,服务器环境
本地测试环境(windows):1.在windows下配置hadoop的环境变量2.拷贝debug工具(winutils.exe)到hadoop目录中的bin目录,注意winutils.exe的版本要 ...
- Angular1.x directive(指令里的)的compile,pre-link,post-link,link,transclude
The nitty-gritty of compile and link functions inside AngularJS directives The nitty-gritty of comp ...