Minimum Path Sum

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.

解题思路:

典型的动态规划。开辟m*n的矩阵path,path[i][j]存放从首元素(grid[0][0])到当前元素(grid[i][j])的最短路径长度。

对于每个元素来说,路径是从上或者从左边来的。

也就是说path[i][j] = min(path[i-1][j]+path[i][j-1]) + grid[i][j]。

注意:初始化第一行第一列。

class Solution {
public:int minPathSum(vector<vector<int> > &grid) {if(grid.empty() || grid[0].empty())return 0;int m = grid.size();int n = grid[0].size();vector<vector<int> > path(m, vector<int>(n, 0));path[0][0] = grid[0][0];for(int i = 1; i < m; i ++)path[i][0] = path[i-1][0] + grid[i][0];for(int i = 1; i < n; i ++)path[0][i] = path[0][i-1] + grid[0][i];for(int i = 1; i < m; i ++){for(int j = 1; j < n; j ++){path[i][j] = min(path[i-1][j], path[i][j-1]) + grid[i][j];}}return path[m-1][n-1];}
};

转载于:https://www.cnblogs.com/ganganloveu/p/3948069.html

【LeetCode】64. Minimum Path Sum相关推荐

  1. python棋盘最短路径_【leetcode】64. Minimum Path Sum 棋盘最短路径

    1. 题目 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...

  2. 【DP】LeetCode 64. Minimum Path Sum

    LeetCode 64. Minimum Path Sum Solution1:标准的动态规划题目 class Solution { public:int minPathSum(vector<v ...

  3. 63. Unique Paths II and 64. Minimum Path Sum

    文章目录 1 63 Unique Paths II 1.1 题目描述 1.2 动态规划解决 2 64. Minimum Path Sum 2.1 题目理解 2.2 动态规划 这一遍刷dp的题目就很轻松 ...

  4. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  5. 【leetcode】931. Minimum Falling Path Sum

    题目如下: Given a square array of integers A, we want the minimum sum of a falling path through A. A fal ...

  6. LeetCode 64. Minimum Path Sum(最小和的路径)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  7. leetcode 64. Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  8. 64. Minimum Path Sum 路径最小总和

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  9. 【leetcode】1007. Minimum Domino Rotations For Equal Row

    题目如下: In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino.  ( ...

  10. 【Leetcode】1925. Count Square Sum Triples

    题目地址: https://leetcode.com/problems/count-square-sum-triples/ 给定一个正整数nnn,求三个数a,b,c≤na,b,c\le na,b,c≤ ...

最新文章

  1. Association Rules 关联规则
  2. INADDR_ANY的理解
  3. Ubuntu上安装Air运行时和Air程序
  4. 收集无良医院清单的开源项目,浏览器插件自动提示无良网站!
  5. 友盟手游开放日全面来袭!奏响手游运营“四部曲”
  6. ESXI 4.0 升级 ESXI 4.1 手记
  7. java 日期注解 xml_Spring xml注解+java注解
  8. Spring AOP根据JdbcTemplate方法名动态设置数据源
  9. WPF中的数据绑定Data Binding使用小结
  10. Sharepoint学习笔记—架构系列
  11. MapReduce综合学习含Wordcount案例
  12. 用 Python 编写一个天气查询应用 pyqt5
  13. 3分钟融云Demo体验:IM即时通讯篇
  14. js文件中使用jstl或者其他标签
  15. java代码解决的问题_java代码规范问题及解决方案
  16. Anders Hejlsberg谈C#、Java和C++中的泛型
  17. linux 镜像 多 网络,Linux内核实现多路镜像流量聚合和复制的方法
  18. 苹果蓝牙耳机使用说明_苹果蓝牙耳机怎么用,其功能及使用方法介绍
  19. Win10 触摸屏 快捷键操作
  20. 小米路由mini刷潘多拉及老毛子固件-详细教程

热门文章

  1. WinForm窗体生命周期
  2. nginx报错unknown directive 锘? in D:\nginx/conf/nginx.conf:3
  3. GCC、VS对C++标准的支持情况总结(转载)
  4. 剪贴板增强工具 Ditto
  5. 06. 重建二叉树(C++版本)
  6. 计算机图形学全代码,计算机图形学作业参考代码
  7. web文件被更改crawlergo怎么解决_hscan:一款集成crawlergo和xray的src漏洞挖掘利器
  8. (day 26 - 广度优先搜索 )剑指 Offer 32 - III. 从上到下打印二叉树 III
  9. ai文件图片连接丢失怎么处理_未来美学丨点亮你的AI技能点(一)
  10. 按键映射_通J1手游按键开箱——这可能是最小的游戏外设了