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.

采用动态规划的方法,dp[i][j] = min(dp[i-1][j],dp[i],[j-1])+a[i][j];

 1 class Solution {
 2 public:
 3     int minPathSum(vector<vector<int>>& grid) {
 4         int m = grid.size();
 5         int n = grid[0].size();
 6         vector<vector<int> > dp(m,vector<int>(n));
 7         for(int i = 0;i < m;i++){
 8             for(int j = 0;j < n ;j++){
 9                 if(i == 0){
10                     if(j == 0){//初始化
11                          dp[i][j] = grid[i][j];
12                     }
13                     else{//i==0 && j!=0
14                         dp[i][j] = dp[i][j-1]+grid[i][j];
15                     }
16                 }
17                 else if(j == 0){//i!=0&& j==0
18                     dp[i][j] = dp[i-1][j] + grid[i][j];
19                 }
20                 else{//i,j!=0
21                     dp[i][j] = min(dp[i-1][j],dp[i][j-1])+grid[i][j];
22                 }
23
24             }
25         }
26         return dp[m-1][n-1];
27     }
28 };

转载于:https://www.cnblogs.com/LaplaceAkuir/p/6264182.html

leetcode 64. Minimum Path Sum相关推荐

  1. 【DP】LeetCode 64. Minimum Path Sum

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

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

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

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

  6. leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】

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

  7. 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. LeetCode 1.Minimum Path Sum 2.Unique Paths I and II

    大家好,我是刘天昊,快到端午节了,今天说两道动态规划的题目(话说动规真的挺难的) 当然这三题是一样的解体思路先看Unique Paths A robot is located at the top-l ...

  9. 64. Minimum Path Sum 最小路径和

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

最新文章

  1. 「AI初识境」深度学习模型中的Normalization,你懂了多少?
  2. 修完 Bug 后脑袋灵光一现 | 每日趣闻
  3. 不是python中用于开发用户界面的第三方库-模拟试卷C
  4. 东风本田crv2020新款混动说明书_能上绿牌,无续航焦虑!2020北京车展混动新车抢先看...
  5. 【IT笔试面试题整理】丑数
  6. 深度学习之卷积神经网络 AlexNet
  7. 自动化集成:Kubernetes容器引擎详解
  8. InheritableThreadLocal类原理简介使用 父子线程传递数据详解 多线程中篇(十八)...
  9. VeriSign 通配型 SSL证书详解
  10. 免费中文Python电子书 转载
  11. 微信小程序01-底部导航栏设置
  12. 理解SSD核心技术FTL
  13. MAXON Cinema 4D R21.207 Win/Mac 中文英文带预设库 C4D R21-三维模型设计软件 安装方法
  14. java爬虫(爬取豆瓣电影排行榜)
  15. 真的有程序员鼓励师,天啦噜!
  16. 凹凸贴图和法线贴图的区别
  17. Godaddy域名申请及设置
  18. 机器自动翻译古文拼音 - 十大宋词 - 念奴娇 赤壁怀古 苏轼
  19. nginx配置websocket支持wss
  20. C语言中所有,C语言所有关键字.doc

热门文章

  1. JZOJ 5925. 【NOIP2018模拟10.25】naive 的瓶子
  2. JZOJ 5392. 【NOIP2017提高A组模拟10.5】Lucky Transformation
  3. JZOJ 1277. 最高的奶牛
  4. centos安装llvm_CentOS 7.0 编译安装LLVM7.0.0版本
  5. 8s保留cpu设置_华为Mate 20 Pro的第二彩蛋-开发者人员选项可以这么设置
  6. python安装sqlite3_Python安装sqlite3
  7. QPushButton 点击信号分析
  8. 掌握python编程语言tensorflow_手把手教你eclipse集成Python语言+Tensorflow环境
  9. matlab中描绘简单曲线和点
  10. Social network Social computing(社会网络和社会计算)