7 Star 34 Fork 23

空無一悟/algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
解法3_动态规划实现.java 1.22 KB
一键复制 编辑 原始数据 按行查看 历史
空無一悟 提交于 2021-09-22 23:47 +08:00 . init
/***********************************************************
* @Description : 64.最小路径和
* https://leetcode-cn.com/problems/minimum-path-sum/
*
* @author : 梁山广(Liang Shan Guang)
* @date : 2020/1/26 14:55
* @email : liangshanguang2@gmail.com
***********************************************************/
package Chapter09DynamicAllocate.Section2ClimbingStairs.LeetCode64最小路径和;
public class 解法3_动态规划实现 {
public int minPathSum(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
int[][] memo = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 && j == 0) {
memo[i][j] = grid[0][0];
continue;
}
if (i == 0) {
memo[i][j] = memo[i][j - 1] + grid[i][j];
continue;
}
if (j == 0) {
memo[i][j] = memo[i - 1][j] + grid[i][j];
continue;
}
memo[i][j] = Math.min(memo[i][j - 1], memo[i - 1][j]) + grid[i][j];
}
}
return memo[m - 1][n - 1];
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lsgwr/algorithms.git
git@gitee.com:lsgwr/algorithms.git
lsgwr
algorithms
algorithms
master

搜索帮助