【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】
【064-Minimum Path Sum(最小路径和)】
【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】
原题
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 x n的方格,每一个元素的值都是非负的。找出从左上角顶点,到右下角顶点和的值最小的路径,返回找到的最小和。
解题思路
分治法,
第一个: S[0][0] = grid[0][0]
第一行: S[0][j] = S[0][j - 1] + grid[0][j]
第一列: S[i][0] = S[i - 1][0] + grid[i][0]
其他情况:S[i][j] = min(S[i - 1][j], S[i][j - 1]) + grid[i][j]
代码实现
算法实现类
public class Solution {
public int minPathSum(int[][] grid) {
// 參数检验
if (grid == null || grid.length < 1 || grid[0].length < 1) {
return 0;
}
int[][] result = new int[grid.length][grid[0].length];
// 第一个
result[0][0] = grid[0][0];
// 第一行
for (int i = 1; i < result[0].length; i++) {
result[0][i] = result[0][i - 1] + grid[0][i];
}
// 第一列
for (int i = 1; i < result.length; i++) {
result[i][0] = result[i - 1][0] + grid[i][0];
}
// 其他情况
for (int i = 1; i < result.length; i++) {
for (int j = 1; j < result[0].length; j++) {
result[i][j] = Math.min(result[i - 1][j], result[i][j - 1]) + grid[i][j];
}
}
return result[result.length - 1][result[0].length - 1];
}
////////////////////////////////////////////////////////////////////////////////////////////////
// 动态归划和分枝限界,以下的方法会超时
////////////////////////////////////////////////////////////////////////////////////////////////
public int minPathSum2(int[][] grid) {
// 參数检验
if (grid == null || grid.length < 1 || grid[0].length < 1) {
return 0;
}
// 用于记录最小的路径各
int[] minSum = {Integer.MAX_VALUE};
int[] curSum = {0};
// 解题
solve(grid, 0, 0, curSum, minSum);
// 返回结果
return minSum[0];
}
public void solve(int[][] grid, int row, int col, int[] curSum, int[] minSum) {
// 假设已经到达终点
if (row == grid.length - 1 && col == grid[0].length - 1) {
curSum[0] += grid[row][col];
// 更新最小的和
if (curSum[0] < minSum[0]) {
minSum[0] = curSum[0];
}
curSum[0] -= grid[row][col];
}
// 还未到达终点,而且在网格内
else if (row >= 0 && row < grid.length && col >= 0 && col < grid[0].length) {
curSum[0] += grid[row][col];
// 当前的和仅仅有不小于记录到的最小路径值才干进行下一步操作
if (curSum[0] <= minSum[0]) {
// 向右走
solve(grid, row, col + 1, curSum, minSum);
// 向下走
solve(grid, row + 1, col, curSum, minSum);
}
curSum[0] -= grid[row][col];
}
}
}
评測结果
点击图片,鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。
特别说明
欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47203311】
【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】的更多相关文章
- 064 Minimum Path Sum 最小路径和
给定一个只含非负整数的 m x n 网格,找到一条从左上角到右下角的可以使数字之和最小的路径.注意: 每次只能向下或者向右移动一步.示例 1:[[1,3,1], [1,5,1], [4,2,1]]根据 ...
- [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] 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 ...
- Leetcode64.Minimum Path Sum最小路径和
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [ [1,3,1], [1,5,1] ...
- 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】
[015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...
- Java for LeetCode 064 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】064. Minimum Path Sum
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
- 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) 20
64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...
随机推荐
- excel2013超链接进不去,提示“您的组织策略不允许...”
搜索regedit 然后找到HKEY_CURRENT_USER->Software->Classes->.html 右键修改或者双击修改数值数据为Htmlfile 关闭之后此窗口,关 ...
- HDU 4333 Contest 4
一开始就想到了扩展KMP,因为只有扩展KMP才是处理后缀的.但忽然短路以为扩展KMP求的是最长公共后缀,囧....又浪费了很多时间,都是对这个算法练得不多 再看那个扩展KMP算法之后,就很确定要的就是 ...
- HDU 2879
利用x<n的信息,可以证得当n为素数时,he[n]=2;同时,若n 为素数,则有HE[N^K]=2;因为若等式成立则有n|x(x-1).抓住这个证即可. 至于符合积性函数,想了很久也没想出来,看 ...
- HBase基本数据操作具体解释
引言 本文档參考最新(截止2014年7月16日)的官方Ref Guide.Developer API编写. 全部代码均基于"hbase 0.96.2-hadoop2"版本号编写.均 ...
- IBM AppScan官方帮助文档错别字缺陷,IBM的測试人员也太粗心了吧
袁术=元素?
- Educational Codeforces Round 6 A. Professor GukiZ's Robot 水
A. Professor GukiZ's Robot Professor GukiZ makes a new robot. The robot are in the point with coor ...
- mpi之MPI_Sendrecv的用法
mpi变成常用命令 编译c程序 gcc 例: gcc -Wall -o my_sa my_sa.c 若要编译c++,需要连接, 加参数 gcc -Wall -o my_sa my_sa.cpp - ...
- 固比固布局 圣杯布局 css实现传统手机app布局
手机app的布局大致上都是头部.内容.底部三部分: 我们需要实现的是头部.底部高度固定:中间内容区域自适应且可以滚动:直接贴代码: css: html,body { width: 100%; heig ...
- [Codeforces 911F] Tree Destruction 解题报告(贪心)
题目链接: http://codeforces.com/contest/911/problem/F 题目大意: 给你一棵树,每次挑选这棵树的两个度数为1的点,加上他们之间的边数(距离),然后将其中一个 ...
- (三)Fegin声明式服务调用
上一篇,讲了SpringClound中的消费者采用Ribbon+Rest来实现,这回我们用组件Feign来实现服务的消费者,Fegin中也是默认集成了Ribbon的;和Eureka结合也能实现负载均衡 ...