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 ...
随机推荐
- android开发 软键盘出现后 防止EditText控件遮挡 总体平移UI
在EditText控件接近底部的情况下 软键盘弹出后会把获得焦点的EditText控件遮挡 无法看到输入信息 防止这种情况发生 就须要设置AndroidManifest.xml的属性 前面的xml信 ...
- 爬虫基础库之requests模块
一.requests模块简介 使用requests可以模拟浏览器请求,比起之前用到的urllib,requests模块的api更加快捷,其实ruquests的本质就是封装urllib3这个模块. re ...
- 顽石系列:CSS实现垂直居中的五种方法
顽石系列:CSS实现垂直居中的五种方法 在开发过程中,我们可能沿用或者试探性地去使用某种方法实现元素居中,但是对各种居中方法的以及使用场景很不清晰.参考的内容链接大概如下: 行内元素:https:// ...
- 关于IDEA导出项目jar包/runnable jar
将项目导出为jar包分为 runnable jar 与 普通jar包 一.导出为普通jar包 该jar包中只有项目源代码, java -cp wordcount.jar 用来运行普通jar包 1.打开 ...
- 【转】通过fio工具,测试SATA,SAS,SSD 读写性能
转自:http://blog.csdn.net/killmice/article/details/42745937
- VC:res协议——从模块中获取资源
你可以从模块中获取一个资源.通过在文件名之前加上res://,你就可以引用一个嵌入在动态链接库资源文件中的HTML页面.
- octotree神器 For Github and GitLab 火狐插件
Code tree for GitHub and GitLabExtension to show code tree for GitHub and GitLab. Useful for develop ...
- Django-虚拟环境设置
Django 虚拟环境virtualenv virtualenv是用来处理多个用python语言进行开发的项目,在同一台机器上部署,不同项目依赖不同第三方库版本所造成的问题. 打个比方,现在你机器上要 ...
- c# 图片 与 BASE64 字符串 互相转换。
using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System. ...
- ZooKeeper服务-操作(API、集合更新、观察者、ACL)
操作 create:创建一个znode(必须要有父节点)delete:删除一个znode(该znode不能有任何子节点)exists:测试一个znode是否存在并且查询它的元数据getACL,setA ...