Minimum Path Sum——LeetCode
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题,用一个二维数组记录到当前位置最小路径和,取决于当前位置的左边和上边的最小路径和,注意处理矩阵最左边和最上边的。
public int minPathSum(int[][] grid) {
if (grid == null || grid[0].length == 0)
return 0;
int rowLen = grid.length;
int colLen = grid[0].length;
int[][] sum = new int[rowLen][colLen];
sum[0][0] = grid[0][0];
for (int i = 0; i < rowLen; i++) {
for (int j = 0; j < colLen; j++) {
if (i > 0 && j > 0)
sum[i][j] = Math.min(sum[i - 1][j], sum[i][j - 1]) + grid[i][j];
else if (i == 0 && j > 0) {
sum[i][j] = sum[i][j - 1] + grid[i][j];
} else if (i > 0 && j == 0) {
sum[i][j] = sum[i - 1][j] + grid[i][j];
}
}
}
return sum[rowLen - 1][colLen - 1];
}
Minimum Path Sum——LeetCode的更多相关文章
- Minimum Path Sum [LeetCode]
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 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 w ...
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- [Leetcode Week9]Minimum Path Sum
Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...
- LeetCode 64. 最小路径和(Minimum Path Sum) 20
64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...
- Leetcode之动态规划(DP)专题-64. 最小路径和(Minimum Path Sum)
Leetcode之动态规划(DP)专题-64. 最小路径和(Minimum Path Sum) 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. ...
- 【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
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
随机推荐
- CreateProcess函数具体解释
CreateProcess说明:WIN32API函数CreateProcess用来创建一个新的进程和它的主线程,这个新进程执行指定的可执行文件. 函数原型:BOOL CreateProcess( ...
- UIDevice 系统信息详解
获取当前设备 //获取当前设备 UIDevice *dev = [UIDevice currentDevice]; 设备和系统基本信息 NSLog(@"设备名称:%@&qu ...
- yii2 控制器里 action 大小写组合造成的路由问题
yii1中, 若存在如下控制器 class BindController extends CController { public function actionGetMobilePhone () { ...
- Linq101-Projection
using System; using System.Linq; namespace Linq101 { class Projection { /// <summary> /// This ...
- asp.net微信开发第五篇----用户分组管理
上一篇已讲解到新建用户分组,移动用户到分组的功能,这一章主要讲解修改分组名称和删除分组 开发者可以使用接口,对公众平台的分组进行查询.创建.修改.删除等操作,也可以使用接口在需要时移动用户到某个分组. ...
- 遍历aspx页面中所有的指定控件
//1.遍历页面中所有的TextBox,并将值设置成String.Empty for (int j = 0; j < this.Controls.Count; j++){ foreac ...
- 直接拿来用的15个jQuery代码片段
1.预加载图片 1 2 3 4 5 6 7 8 9 10 11 12 (function($) { var cache = []; // Arguments are image paths r ...
- UIScrollView设置了contentSize后还是没办法滚动?
1.最常见的原因是 contentSize 这个属性,比uiscrollview的frame要小, 无需滚动, 自然就滚动不了. scrollenabled 这个属性,标识着是否允许滚动,要言设成ye ...
- Swift - 32 - 函数类型
//: Playground - noun: a place where people can play import UIKit func add(a:Int, b:Int) -> Int { ...
- [转载]C++中声明与定义的区别
C++学了这么多年你知道为什么定义类时,类的定义放在.h文件中,而类的实现放在cpp文件中.它们为什么能够关联到一起呢?你知道什么东西可以放在.h文件中,什么不能.什么东西又可以放在cpp文件中.如果 ...