[leetcode]64Minimum Path Sum 动态规划
/**
* 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.
*/
/*
* 动态规划题目,思路是把每一个点的最小和求出来,最后返回右下角的点。
* 每个点的最小和就是比较它左边和上边的点,把较小的那个和本身加起来
* 首先应该吧最左边列和最上边一行的点的最小和求出,因为后边要用到,且他们的最小和求法和中间的不一样
* 难点:初始值很多,是左边一列和上边一行所有的点,都可以直接求出*/
public class Q64MinimumPathSum {
public static void main(String[] args) { }
public int minPathSum(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
//求出最左边一行的每个点对应的最小和
for (int i = 1; i < m; i++) {
grid[i][0] += grid[i-1][0];
}
//最上边行的
for (int i = 1; i < n; i++) {
grid[0][i] += grid[0][i-1];
}
//前两个是判断特殊情况
if (m == 1)
return grid[0][n-1];
else if (n == 1)
return grid[m-1][0];
//动态规划主体
else
{
int temp;
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
//求该点的最小和
temp = Math.min(grid[i-1][j],grid[i][j-1]);
grid[i][j] += temp;
}
}
//右下角的点
return grid[m-1][n-1];
}
}
}
[leetcode]64Minimum Path Sum 动态规划的更多相关文章
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] 437. Path Sum III 路径和 III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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 437. Path Sum III (路径之和之三)
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)
Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...
随机推荐
- 自学linux——18.FTP服务器的搭建
Centos7下FTP服务器的搭建 一.FTP的作用 文件传输协议(File Transfer Protocol,FTP),是一种在互联网中进行文件传输的协议,基于客户端/服务器模式,默认使用 20. ...
- WriteFile,CreateFile,ReadFile
原文链接:https://blog.csdn.net/Jeanphorn/article/details/44982273 将数据写入一个文件.该函数比fwrite函数要灵活的多.也可将这个函数应用于 ...
- Python中自定义类未定义__lt__方法使用sort/sorted排序会怎么处理?
在<第8.23节 Python中使用sort/sorted排序与"富比较"方法的关系分析>中介绍了排序方法sort和函数sorted在没有提供key参数的情况下默认调用 ...
- jmeter使用中的问题
1.响应乱码 step1:指定请求节点下,新建后置控制器"BeanShell PostProcessor" step2:其脚本框中输入以下代码,保存 //获取响应代码Unicode ...
- MySQL存储引擎:MyISAM和InnoDB的区别
MyISAM和InnoDB的区别 定义 InnoDB:MySQL默认的事务型引擎,也是最重要和使用最广泛的存储引擎.它被设计成为大量的短期事务,短期事务大部分情况下是正常提交的,很少被回滚.InnoD ...
- react-admin-plus 正式开源, 欢迎star
简介 基于react.ant-ui.typescript的前端微服务框架.欢迎star. 在线地址 在线demo 项目介绍 沉淀了几个月的时间,这款框架终于正式的和大家见面了! 先说一下我做这 ...
- NOI 2020 D1T3 本人题解
我看了出题人本题的做法,感觉很难写,就自己胡了一个\(O((n + m) \sqrt n)\)的做法. 第一步我的想法与出题人一样,都是考虑容斥降维.对第\(i\)组询问,我们枚举两个事件中较大的一个 ...
- Python测试框架unittest
Python测试框架unittest 一.unittest框架 (1)unittest特点 1.python自带的单元测试框架,不需要安装 2.用例执行互不干扰 3.提供不同范围的setUp和tear ...
- 【Jmeter中,保存测试结果xml时报 error loading results file -see log file 问题的处理办法】
使用JMeter测试并发保存测试文件时报错:Error loading results file - see file log解决办法:新建一个文本文件(什么类型都可以),在文件中加上<?xml ...
- MySQL锁(一)全局锁:如何做全库的逻辑备份?
数据库锁设计的初衷是处理并发问题,这也是数据库与文件系统的最大区别. 根据加锁的范围,MySQL里大致可以分为三种锁:全局锁.表锁和行锁.接下来我们会分三讲来介绍这三种锁,今天要讲的是全局锁. 全局锁 ...