LeetCode(64) Minimum Path Sum
题目
Total Accepted: 47928 Total Submissions: 148011 Difficulty: Medium
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.
分析
这道题目是求最小路径和,与LeetCode 62以及LeetCode63本质相同,只需要把存储路径数目的数组改为存储当前最小路径和即可。
AC代码
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
if (grid.empty())
return 0;
//求当前矩阵的行数、列数
int m = grid.size();
int n = grid[0].size();
vector<vector<int> > sum(m, vector<int>(n, 0));
//记录首元素和
sum[0][0] = grid[0][0];
int minSum = sum[0][0];
for (int i = 1; i < m; i++)
{
sum[i][0] = sum[i - 1][0] + grid[i][0];
//此时路径和唯一,也是最小路径和
}//for
for (int j = 1; j < n; j++)
{
sum[0][j] = sum[0][j - 1] + grid[0][j];
//此时路径和唯一,也是最小路径和
}//for
for (int i = 1; i < m; i++)
{
for (int j = 1; j < n; j++)
{
sum[i][j] = min(sum[i - 1][j], sum[i][j - 1]) + grid[i][j];
}//for
}//for
return sum[m - 1][n - 1];
}
};
LeetCode(64) Minimum Path Sum的更多相关文章
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
- 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 ...
- LeetCode(76) Minimum Window Substring
题目 Given a string S and a string T, find the minimum window in S which will contain all the characte ...
- LeetCode(71) Simplify Path
题目 Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/&quo ...
- LeetCode(111) Minimum Depth of Binary Tree
题目 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the s ...
- LeetCode(64):最小路径和
Medium! 题目描述: 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [ [1 ...
- Leetcode之动态规划(DP)专题-64. 最小路径和(Minimum Path Sum)
Leetcode之动态规划(DP)专题-64. 最小路径和(Minimum Path Sum) 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. ...
- [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(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
随机推荐
- 【转】Postman 使用方法详解
1.Postman背景介绍 用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的Firebug等网页调试工具.今天给大家介 ...
- [CF1109F]Sasha and Algorithm of Silence's Sounds
题意 有一个\(n*m\)的网格,每个格子有一个数,为\(1\)~\(n * m\)的排列 一个区间\((1<=l<=r<=n*m)\)是好的,当且仅当:数值在该区间内的格子,构成一 ...
- Monkey Banana Problem LightOJ - 1004
Monkey Banana Problem LightOJ - 1004 错误记录: 1.数组开小2.每组数据数组没有清空 #include<cstdio> #include<cst ...
- 水题 Gym 100553K Knockout Racing
题目传送门 /* 题意:有若干个点在一个区间内来回移动,1m/s. 水题:n^2的复杂度能解决,注意时间可能大于一个周期,要取模 */ #include <cstdio> #include ...
- 题解报告:hdu 1212 Big Number(大数取模+同余定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1212 Problem Description As we know, Big Number is al ...
- SpringCloud开发学习总结(六)—— 结合注解的AOP示例
面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型.利用AOP ...
- python_one-day
python入门_(1) 作者:_晓冬 归档:学习笔记 2017/9/9 目 录 第1章 练习... 1 1.1 格式化输出... 1 1.2 流程控制if..else. 1 1.3 流程控制whi ...
- $.ajax json 在本地正常 上传服务器不正常
$.ajax( { url:"url",// 跳转到 action data:{name ...
- Dev之GridControl详解
Dev控件中的表格控件GridControl控件非常强大.不过,一些细枝末节的地方有时候用起来不好找挺讨厌的.使用过程中,多半借助Demo和英文帮助文档.网上具体的使用方法也多半零碎.偶遇一个简单而且 ...
- WP7 开发资料
前言 离Windows Phone 7正式发布已过去几个月了,但国内关于Windows Phone 7的中文书籍资料太少了,大多数是英文资料,一本真正的中文开发教程书都没有, 要啃英文资料对大部分的开 ...