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 ...
随机推荐
- jQuery笔记之Easing Plugin
jQuery easing 使用方法首先,项目中如果需要使用特殊的动画效果,则需要在引入jQuery之后引入jquery.easing.1.3.js<script type="text ...
- Lodash 方法库 原生实现 待完结。。。
'use strict' let _ = { // 将数组(array)拆分成多个 size 长度的区块,并将这些区块组成一个新数组. 如果array 无法被分割成全部等长的区块,那么最后剩余的元素将 ...
- 使用Docker Compose编排微服务
一般微服务架构会包含若干个微服务,而每个微服务可以有多个实例,如果每个微服务都有手动启停,那么效率就很低.维护量比较大. 所以我们可以使用Docker Compose来轻松.高效地管理容器. 一.安装 ...
- nginx_uWSGI_django_virtualenv_supervisor发布web服务器
nginx_uWSGI_django_virtualenv_supervisor发布web服务器 nginx 导论 123456789101112131415161718192021222324252 ...
- _bzoj1798 [Ahoi2009]Seq 维护序列seq【线段树 lazy tag】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1798 注意,应保证当前节点维护的值是正确的,lazy tag只是一个下传标记,在下传时应即时 ...
- (三)python函数式编程
一.高阶函数 高阶函数英文叫Higher-order function.什么是高阶函数?我们以实际代码为例子,一步一步深入概念. 变量可以指向函数 结论:函数本身也可以赋值给变量,即:变量可以指向函数 ...
- 题解报告:hdu 1236 排名
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1236 Problem Description 今天的上机考试虽然有实时的Ranklist,但上面的排名 ...
- C# winform与Javascript的相互调用[转]
原文链接<html> <head> <meta http-equiv="Content-Language" content="zh-cn&q ...
- 017:COM1无法打开
重新安装系统以后,COM1无法正常打开,重启以后也是如此.到设备管理器下,禁用COM1然后重启可以正常使用.修改COM1为别的COM号,重启以后可以正常使用.用Pcomm控件,打开该串口,错误号是-8 ...
- Toast解析
课程Demo public class MainActivity extends AppCompatActivity { private Button bt1; private Button bt2; ...