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 ...
随机推荐
- 深入浅出索引--Mysql45讲笔记记录 打卡day3
看了极客时间的mysql45讲记录一下自己理解的关于索引部分 为什么会有索引呢? 答:索引就像书的目录一样,可以让你快速知道你要看的部分在多少页.换句话说,索引就是为了提高数据库的查询效率. 索引的数 ...
- React实战之将数据库返回的时间转换为几分钟前、几小时前、几天前的形式。
React实战之将数据库返回的时间转换为几分钟前.几小时前.几天前的形式. 不知道大家的时间格式是什么样子的,我先展示下我这里数据库返回的时间格式 ‘2019-05-05T15:52:19Z’ 是这个 ...
- Oracle11.2.0.1升级到11.2.0.3
Oracle数据库升级也并非简单的事,这篇博客,博主对Oracle那点事做了较详细的介绍: http://blog.itpub.net/9599/viewspace-473003/ 我还属于Oracl ...
- 【杂谈】小记一个ios11的bug
前段时间,除了apple发布了新的硬件之外,同步还发布了新的操作系统,IOS11,当大家都将注意力聚焦在那个奇怪的刘海该如何适配的时候,笔者的项目在适配IOS11却出现了其他的问题. 众所周知,IOS ...
- 通过IDEA制作包含Java应程序的Docker镜像
IDEA官网在IDEA中把Java App制作成Docker镜像并启动一个容器运行 在idea上使用docker作为java的开发环境[][] ubuntu+docker+docker-compose ...
- Linux环境下SolrCloud集群环境搭建关键步骤
Linux环境下SolrCloud集群环境搭建关键步骤. 前提条件:已经完成ZooKeeper集群环境搭建. 一.下载介质 官网下载地址:http://www.apache.org/dyn/close ...
- C# winform与Javascript的相互调用[转]
原文链接<html> <head> <meta http-equiv="Content-Language" content="zh-cn&q ...
- shell 2 解析
---- shell 3 /home/oracle/utility/macro/call_autopurge_arch.sh Description: Call purge archive log f ...
- ABP Zero最新版源码
获取专业版源码 官网 学习版源码
- idea下关联spark源码环境(转)
0.环境: java 1.8 scala 2.11.8 maven 3.5.0 idea 2017 spark 2.2.0 1完成以下配置 java环境变量 scala环境变量 maven setti ...