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的更多相关文章

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

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

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

  4. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

  5. [Leetcode Week9]Minimum Path Sum

    Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...

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

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

  7. Leetcode之动态规划(DP)专题-64. 最小路径和(Minimum Path Sum)

    Leetcode之动态规划(DP)专题-64. 最小路径和(Minimum Path Sum) 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. ...

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

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

随机推荐

  1. [转] weak_ptr解决shared_ptr环状引用所引起的内存泄漏

    http://blog.csdn.net/liuzhi1218/article/details/6993135 循环引用: 引用计数是一种便利的内存管理机制,但它有一个很大的缺点,那就是不能管理循环引 ...

  2. SSL证书制作

    1.创建根证书秘钥文件(自己做CA)root.key: openssl genrsa -out root.key -aes256 2048 2.创建根证书的申请文件root.csr openssl r ...

  3. 异步tcp通信——APM.ConsoleDemo

    APM测试 俗话说麻雀虽小,五脏俱全.apm虽然简单,但是可以实现单机高性能消息推送(可以采用redis.kafka等改造成大型分布式消息推送服务器). 测试demo: using System; u ...

  4. Python Socket单线程+阻塞模式

    Python之旅]第五篇(二):Python Socket单线程+阻塞模式 python Socket单线程 Socket阻塞模式 串行发送 摘要:  前面第五篇(一)中的一个Socket例子其实就是 ...

  5. 转载:C# 中的委托

    原文地址  http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx 感谢博主分享! 引言 委托和 ...

  6. 【开源java游戏框架libgdx专题】-03-项目开发与调试

    创建libgdx项目 下载项目配置工具 gdx-setup.jar 生成项目 导入Eclipse File -> Import -> Gradle -> Gradle Project ...

  7. php创建读取 word.doc文档

    创建文档; <?php $html = "this is question"; for($i=1;$i<=3;$i++){ $word = new word(); $w ...

  8. XML参数转换为Object,并转换为List或DataTable

    demo效果:

  9. Android中的缓存机制与实现

    分步阅读 Android开发本质上就是手机和互联网中的web服务器之间进行通信,就必然需要从服务端获取数据,而反复通过网络获取数据是比较耗时的,特别是访问比较多的时候,会极大影响了性能,Android ...

  10. HTTP简单理解

    自己最近正在看 <Java Web 整合开发实战>这本书, 看到HTTP协议,感觉写的挺明白了,就把书中内容总结了下,记录在此. 超文本传输协议(HyperText Transfer Pr ...