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.

思路:由于只能向两个方向走,瞬间就没有了路线迂回的烦恼,题目的难度大大的降低了。先得到到达最上边和最左边的最小路径,其他的就从上面点和左边点中选一个路径短的路径加上。

当然,也可以空间上更简单一些,只缓存一行的数据,然后一行行的更新。但我懒得写了...

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <stack>
  6. using namespace std;
  7.  
  8. class Solution {
  9. public:
  10. int minPathSum(vector<vector<int> > &grid) {
  11. if(grid.size() == )
  12. {
  13. return ;
  14. }
  15. vector<vector<int> > ans(grid.size(), vector<int>(grid[].size(), ));
  16. ans[][] = grid[][];
  17. for(int i = ; i < grid.size(); i++)
  18. {
  19. ans[i][] = ans[i - ][] + grid[i][];
  20. }
  21. for(int j = ; j < grid[].size(); j++)
  22. {
  23. ans[][j] = ans[][j - ] + grid[][j];
  24. }
  25. for(int i = ; i <grid.size(); i++)
  26. {
  27. for(int j = ; j <grid[].size(); j++)
  28. {
  29. ans[i][j] = min(ans[i - ][j], ans[i][j - ]) + grid[i][j];
  30. }
  31. }
  32. return ans.back().back();
  33. }
  34. };

【leetcode】Minimum Path Sum(easy)的更多相关文章

  1. 【leetcode】Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  2. 【题解】【矩阵】【DP】【Leetcode】Minimum Path Sum

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

  3. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  4. 【leetcode】437. Path Sum III

    problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完

  5. 【LeetCode】113. Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  6. 【LeetCode】666. Path Sum IV 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

  7. 【Leetcode】【Medium】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. 【LeetCode】437. Path Sum III 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + DFS BFS + DFS 日期 题目地 ...

  9. 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...

随机推荐

  1. [译]Mongoose指南 - Plugin

    Schema支持插件, 这样你就可以扩展一些额功能了 下面的例子是当document save的时候自定更新最后修改日期的出插件 // lastMod.js module.exports = expo ...

  2. 利用UIActivityController调用ios系统自带的分享功能,实现微信发布多图的功能

    通过一番查找以后找到一个类UIActivityController,可以调用系统的social.framework中的分享接口.看下面的图就知道了,这个还是挺常见的 微信发布多图 借鉴了CSDN上的一 ...

  3. zookeeper集群配置与启动——实战

    1,准备: A:三台linxu服务器: 10.112.29.177 10.112.29.172 10.112.29.174 命令 hostname 得到每台机器的 hostname vm-10-112 ...

  4. golang的json操作

    package main import ( "encoding/json" "fmt" "os" ) type ConfigStruct s ...

  5. Chrome Restful Api 测试工具 Postman-REST-Client离线安装包下载,Axure RP Extension for Chrome离线版下载

    [Postman for Chrome 离线下载] Postman-REST-Client离线安装包,可直接在Chrome浏览器本地安装使用,可模拟各种http请求,Restful Api测试, CS ...

  6. Swift2.1 语法指南——可空链式调用

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  7. jquery左右滑动效果的实现

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. java中二进制和流的相互转换

    流转二进制 public static byte[] toByteArray(InputStream in) throws IOException { ByteArrayOutputStream ou ...

  9. Xcode 7 App Transport Security has blocked a cleartext HTTP 报错解决办法

    Xcode 7 创建新项目用到 UIWebView 发送请求时,报下面的错: “App Transport Security has blocked a cleartext HTTP (http:// ...

  10. CSS3.0盒模型display:box;的使用

    CSS3.0盒模型display:-webkit-box;的使用 box-flex是css3新添加的盒子模型属性,它的出现可以解决我们通过N多结构.css实现的布局方式.经典的一个布局应用就是布局的垂 ...