LeetCode OJ: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 的网格,求其左上角到右下角的最短路径和:
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(最小路径和)的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 064 Minimum Path Sum 最小路径和
给定一个只含非负整数的 m x n 网格,找到一条从左上角到右下角的可以使数字之和最小的路径.注意: 每次只能向下或者向右移动一步.示例 1:[[1,3,1], [1,5,1], [4,2,1]]根据 ...
- Leetcode64.Minimum Path Sum最小路径和
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [ [1,3,1], [1,5,1] ...
- [Leetcode Week9]Minimum Path Sum
Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...
- 【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 ...
- 【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 ...
- 【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 ...
随机推荐
- OFMessageDecoder 分析
OFMessageDecoder 继承了抽象类 FrameDecoder.FrameDecoder 会将接收到的ChannelBuffers 转换成有意义的 frame 对象.在基于流的传输 ...
- 20170330 ABAP代理生成
在线文档ABAP代理生成, ABAP 代理生成(事物SPROXY)使您能够通过使用企业服务资源中企业服务资源库中 的接口描述在SAP系统中生成ABAP代理对象. ABAP代理生成概览: 1.代理标识, ...
- JAVA项目中常用的异常知识点总结
JAVA项目中常用的异常知识点总结 1. java.lang.nullpointerexception这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用 ...
- eval in Shell
语法:eval cmdLine eval会对后面的cmdLine进行两遍扫描,如果第一遍扫描后,cmdLine是个普通命令,则执行此命令: 如果cmdLine中含有变量的间接引用,则保证间接引用的语义 ...
- Git处理 行结束符
Dealing with line endings (Windows) 如果你正在使用Git在GitHub上和别人协作的话,确保Git处理行结束符的配置已经正确配置了. 每次在键盘上按下return键 ...
- python并发编程之多进程2-(数据共享及进程池和回调函数)
一.数据共享 1.进程间的通信应该尽量避免共享数据的方式 2.进程间的数据是独立的,可以借助队列或管道实现通信,二者都是基于消息传递的. 虽然进程间数据独立,但可以用过Manager实现数据共享,事实 ...
- Spring:笔记整理(1)——HelloWorld
Spring:笔记整理(1)——HelloWorld 导入JAR包: 核心Jar包 Jar包解释 Spring-core 这个jar 文件包含Spring 框架基本的核心工具类.Spring 其它组件 ...
- Python 1 数据类型的操作
一.数字(Number) 1.数学函数: 函数 返回值 ( 描述 ) abs(x) 返回数字的绝对值,如abs(-10) 返回 10 ceil(x) 返回数字的上入整数,如math.ceil(4.1) ...
- 小数据池、is 和 ==的区别
小数据池,在一定情况下出现内存共享(只有int 和 str 才有的) is 和 ==的区别 id() 打印数据的地址 a = 'hello' b = 'hello' print(a = ...
- POJ - 2464 Brownie Points II 【树状数组 + 离散化】【好题】
题目链接 http://poj.org/problem?id=2464 题意 在一个二维坐标系上 给出一些点 Stan 先画一条过一点的水平线 Odd 再画一条 过Stan那条水平线上的任一点的垂直线 ...