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 的网格,求其左上角到右下角的最短路径和:

DP问题,计算式是:min(ret[i][j]) = min(ret[i - 1][j], ret[i][j - 1]) + input[i][j];

代码如下:

 class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
if(grid.size() == || grid[].size() == ) return ;
int szHor = grid.size();
int szVer = grid[].size();
vector<vector<int>> ret = grid;
ret[][] = grid[][]
for(int i = ; i < szHor; ++i){
ret[i][] = ret[i - ][] + grid[i][];
} for(int i = ; i < szVer; ++i){
ret[][i] = ret[][i - ] + grid[][i];
} for(int i = ; i < grid.size(); ++i){
for(int j = ; j < grid[].size(); ++j){
ret[i][j] = min(ret[i - ][j], ret[i][j - ]) + grid[i][j];
}
}
return ret[szHor - ][szVer - ];
}
};

简单的DP问题,java版本代码如下所示:

 public class Solution {
public int minPathSum(int[][] grid) {
if(grid.length == 0 || grid[0].length == 0)
return 0;
int szRol = grid.length;
int szCol = grid[0].length;
int [][] ret = new int [szRol][szCol];
ret[0][0] = grid[0][0];
for(int i = 1; i < szRol; ++i){
ret[i][0] = ret[i-1][0] + grid[i][0];
}
for(int i = 1; i < szCol; ++i){
ret[0][i] = ret[0][i-1] + grid[0][i];
}
for(int i = 1; i < szRol; ++i){
for(int j = 1; j < szCol; ++j){
ret[i][j] = Math.min(ret[i-1][j], ret[i][j-1]) + grid[i][j];
}
}
return ret[szRol-1][szCol-1];
}
}

LeetCode OJ:Minimum Path Sum(最小路径和)的更多相关文章

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

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

  4. LeetCode OJ:Path Sum(路径之和)

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  5. 064 Minimum Path Sum 最小路径和

    给定一个只含非负整数的 m x n 网格,找到一条从左上角到右下角的可以使数字之和最小的路径.注意: 每次只能向下或者向右移动一步.示例 1:[[1,3,1], [1,5,1], [4,2,1]]根据 ...

  6. Leetcode64.Minimum Path Sum最小路径和

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

  7. [Leetcode Week9]Minimum Path Sum

    Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...

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

  9. 【leetcode】Minimum Path Sum(easy)

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

  10. 【LeetCode OJ】Path Sum II

    Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...

随机推荐

  1. self-awareness is key in changing the way you think

    1: controlling the way you think is a manageable process The good news is that you have control over ...

  2. 'is' in Python

    在Python中应该避免将“is”运算符用于比较 像“数值”和“字符串”这种不可变的值.由于Python内部操作这些对象的方式,使得对这些对象使用“is”运算符的结果将是不可预测的. 下面以两个例子加 ...

  3. windows安装pywin32

    下载旧版 https://sourceforge.net/projects/pywin32/files/pywin32/ 下载新版 https://github.com/mhammond/pywin3 ...

  4. SourceTree的基本使用---基本介绍/本地开发

    转载自https://www.cnblogs.com/tian-xie/p/6264104.html 1. SourceTree是什么 拥有可视化界面的项目版本控制软件,适用于git项目管理 wind ...

  5. C# TreeView,递归循环数据加载到treeView1中

    TblAreaBLL bll = new TblAreaBLL(); private void button1_Click(object sender, EventArgs e) { LoadData ...

  6. Objective-C系列总结之基础知识

    //第一个程序示例 #import <Foundation/Foundation.h> int main(int argc,const char * argv[]) { @autorele ...

  7. 【Head First Servlets and JSP】笔记6:什么是响应首部 & 快速搭建一个简单的测试环境

    搭建简单的测试环境 什么是响应首部 最简单的响应首部——Content-Type 设置响应首部 请求重定向与响应首部 在浏览器中查看Response Headers 1.先快速搭建一个简单的测试环境, ...

  8. R基本画图

    参考内容:闻博,R语言的绘图功能及应用案例  https://wenku.baidu.com/view/80f22fa50029bd64783e2c22.html R画图是以函数操作为基本的画图模式. ...

  9. Classloader机制

    1.概述? 类加载器:负责.class文件加载到内存中,并为之生成对应的Class对象,也就是字节码对象.这样就可以使用这个类中的成员变量和方法了.而被加载到内存中的class文件就会变成class对 ...

  10. springmvc时间类型值传输映射

    背景:springmvc4.3.2+spring4.3.2+mybatis3.4.1 当前台传递的参数有时间类型时,封装的vo对象也有对应的时间类型与之对象, 但是如果此时用对象去接收后台会报错,类型 ...