【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(最小路径和)】的更多相关文章

  1. 064 Minimum Path Sum 最小路径和

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

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

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

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

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

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

  6. 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】

    [015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...

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

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

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

  10. LeetCode 64. 最小路径和(Minimum Path Sum) 20

    64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...

随机推荐

  1. nodejs安装与概述

    第一部分:安装与测试 1 官方下载地址 https://nodejs.org/en/ 2 测试是否安装成功? window下打开CMD窗口   输入:node -v  => 显示安装的nodej ...

  2. MyBatis学习总结(6)——调用存储过程

    一.提出需求 查询得到男性或女性的数量, 如果传入的是0就女性否则是男性 二.准备数据库表和存储过程 create table p_user( id int primary key auto_incr ...

  3. URAL 1517 Freedom of Choice

    Freedom of Choice Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on Ural. Orig ...

  4. Spring中基于Java的配置@Configuration和@Bean用法 (转)

    spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...

  5. 阿里云server部署架构

    近期要上马一个项目,客户要求所有部署到阿里云的server,做了一个阿里云的部署方案. 上图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc21hbGx ...

  6. Android在程序中浏览网页

    本文是自己学习所做笔记,欢迎转载.但请注明出处:http://blog.csdn.net/jesson20121020 有时须要在程序中浏览一些网页.当然了能够通过调用系统的浏览器来打开浏览.可是大多 ...

  7. poj3249 Test for job 【图的DAG dp】

    #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> ...

  8. SQL语句将某字段查询出以逗号隔开

    MySQL的sql语句有好多能够省去server端的复杂处理 1.group_concat 这玩意儿能够实现  将一个字段如id查询出来   成为这种格式:121,122,123,124,125,12 ...

  9. 安卓APP载入HTML5页面解决方式总结

    因为H5页面在移动端的兼容性及扩展性方面体现出来的优势,又兼得APP中植入H5页面相应用的灵活性有大大的提升(如活动.游戏的更新等).APP开发不可避免的须要载入一些H5页面.但安卓client对网页 ...

  10. hdu2546 饭卡 01-背包问题

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 Problem ...