【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 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...
随机推荐
- 表单标签 fieldset legent
书写表单时可以提供简单样式的标签 <fieldset> <legent></legent> <input type="text" > ...
- Spring 7大功能模块的作用
1. Spring 7大功能模块的作用 1) 核心容器(Spring core) 核心容器提供Spring框架的基本功能.Spring以bean的方式组织和管理Java应用中的各个组件及其关系 ...
- ASP.NET-AuthorizeAttribute做身份验证操作
代码顺序为:OnAuthorization-->AuthorizeCore-->HandleUnauthorizedRequest 如果AuthorizeCore返回false时,才会走H ...
- 为什么选性别会导致兴趣都选中-vue
为什么选性别会导致兴趣都选中-vue <%@ page language="java" import="java.util.*" pageEncoding ...
- jQuery——map()函数以及它的java实现
map()函数小简单介绍 map()函数一直都是我觉得比較有用的函数之中的一个,为什么这么说呢? 先来考虑一下.你是否碰到过下面场景:须要遍历一组对象取出每一个对象的某个属性(比方id)而且用分隔符隔 ...
- 小记 SqlHelper
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Web;u ...
- Android应用内加载pdf的方法?
[可行] 最直接的方式下载到本地, 然后调用相关应用打开 [可行,有局限] 使用 webview 加载, 需要在 原url 前面加上 http://docs.google.com/gview?url= ...
- 细述 Java垃圾回收机制→Types of Java Garbage Collectors
细述 Java垃圾回收机制→Types of Java Garbage Collectors 转自:https://segmentfault.com/a/1190000006214497 本文非原创, ...
- JavaScript原型链:prototype与__proto__
title: 'JavaScript原型链:prototype与__proto__' toc: false date: 2018-09-04 11:16:54 主要看了这一篇,讲解的很清晰,最主要的一 ...
- css文字换行问题white-space:pre-line或者white-space:pre-wrap,解决word-wrap:break-word解决不了的
想让文字换行必须要写的那几个css样式就略过了.当一行文字是数字或字母时或者数字字母组合时会出现不换行局面,这时候加个word-wrap:break-word:就基本可以解决但是有种情况是它解决不了的 ...