题目

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];
}
};

GitHub测试程序源码

LeetCode(64) Minimum Path Sum的更多相关文章

  1. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

  2. 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 ...

  3. 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 ...

  4. LeetCode(71) Simplify Path

    题目 Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/&quo ...

  5. 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 ...

  6. LeetCode(64):最小路径和

    Medium! 题目描述: 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [   [1 ...

  7. Leetcode之动态规划(DP)专题-64. 最小路径和(Minimum Path Sum)

    Leetcode之动态规划(DP)专题-64. 最小路径和(Minimum Path Sum) 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. [POI2007]大都市meg

    Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...

  2. Cunning Gena CodeForces - 417D

    Cunning Gena CodeForces - 417D 题意 先将小伙伴按需要的监视器数量排序.然后ans[i][j]表示前i个小伙伴完成j集合内题目所需最少钱.那么按顺序枚举小伙伴,用ans[ ...

  3. 使用HttpClient携带pfx证书调用HTTPS协议的WebService

    调用第三方服务时,厂商提供了一个WSDL文件.调用的地址和一个后缀为pfx的证书文件,通过SOUPUI记载证书是可以正常调用WebService服务,那么如何将该服务转换为代码呢? 咨询了厂商的支持, ...

  4. 善用oss客户端工具

    有个需求:需要我到阿里oss上下载ts文件 估摸了一下100多个只占了6分之一的时间,全下下来得700多个 还不算上正在运行的 正当我手动一个一个点的时候: 100个 总算点完了 全部在桌面是摆着: ...

  5. JDBC连接Oracle工具类

    import java.sql.*;import java.util.ResourceBundle; /** * jdbc工具类,负责: * 1. 加载/注册数据库驱动程序 * 2. 获取数据库连接 ...

  6. Jboss服务器使用

    一.作者前言 早上坐地铁的时候,阅览about JAVA.了解到一个程序猿,对于服务器的使用,最起码的熟悉那么几种,例如tomcat,jboss,weblogic,websphere,还有Nginx. ...

  7. Toolbar自定义布局

    Toolbar如何使用想必大家清楚地很,实际开发中标题栏的样式各色各样,因此其基本样式便不能满足我们的需求,这就需要我们自定义布局.打开ToolBar源码我们发现它继承ViewGroup,这就表示我们 ...

  8. 解决国内无法安装android sdk的问题

    在使用 Android SDK Manager 的时候,主要会连接到两个地址 dl.google.com 和 dl-ssl.google.com,key发现这两个地址都是无法正常访问的,如何解决呢? ...

  9. centos7.2密码在单用户下面的修改

    centos7.2在但用户模式下面的修改 1.开机启动 2.grub模式按E健 3.Linux16行的"ro"修改为 "rw init=/sysroot/bin/sh&q ...

  10. 自定义对话框(jDialog)

    [配置项]jDialog options点击收起 一.接口功能 jDialog的默认配置项,本组件提供的所有对话框,都可以通过修改这些配置项来实现不同的效果. 二.详细配置项 /** * 对话框的默认 ...