/**
* 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 动态规划的更多相关文章

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

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

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

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

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

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

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

  8. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

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

随机推荐

  1. python将对象写入文件,以及从文件中读取对象

    原文地址: http://www.voidcn.com/article/p-fqtqpwxp-wo.html 写入文件代码: >>> import sys, shelve >& ...

  2. 创建实验楼课程app模块以及配置图片路径

    1.创建course模型 1.1 创建用户模型course python ../manage.py startapp course # 创建course模型 1.2 在setting.py中注册cou ...

  3. 网络拓扑实例10:MSTP+VRRP组合组网

    组网图形 MSTP+VRRP组合简介 网络中部署VRRP负载分担时,多台设备同时承担业务,每个虚拟设备都包括一个Master设备和若干个Backup设备.如果为了接入备份需要同时部署冗余链路,则需要部 ...

  4. 2017 Mid Central Regional G. Faulty Robot(dfs + 尬模)

    这道题看上去太像tarjan缩点了,我一上去本来想把所有的环给缩掉然后统计答案,后来发现哦,这道题不是这么回事儿. 给出黑边红边,一次性走至多只能走一次黑边,问有多少个点可以走到,并且让机器人停下来, ...

  5. Codeforces Round 665 (div2)

    2020.8.22 装修完了我的博客,喜欢这个造型,挂上友链就更好了 昨天cf就是一个彻头彻尾的悲剧,本来能上蓝,结果因为在A题耽误时间过多导致掉了30分,不过没关系,这算是一个小波动吧,影响不了什么 ...

  6. 第7.10节 Python类中的实例变量定义与使用

    一.    引言 在前面章节已经引入介绍了类变量和实例变量,类体中定义的变量为类变量,默认属于类本身,实例变量是实例方法中定义的self对象的变量,对于每个实例都是独有数据,而类变量是该类所有实例共享 ...

  7. 转:Python2字符编码问题汇总

    这篇文章的部分问题在Python3以后不再存在,老猿只是觉得文章的部分内容还是有参考价值,因此在此原文转发连接: Python2字符编码问题汇总

  8. PyQt(Python+Qt)学习随笔:QTableWidget的构造方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTableWidget有2个构造方法: QTableWidget(QWidget parent = ...

  9. PyQt(Python+Qt)学习随笔:Qt Designer中toolBar的orientation属性和iconSize属性

    orientation属性 orientation属性用于确认工具栏是水平方向还是垂直方向,这个属性对于QMainWindow中的工具栏来说没有意义,因为QMainWindow中的工具栏支持在上下左右 ...

  10. PyQt(Python+Qt)学习随笔:Qt Designer中的menu菜单及menu bar菜单栏

    菜单由menu bar菜单栏和menu菜单两部分构成,分别对应类QMenuBar和QMenu. menuBar是包含一系列下拉菜单项组成,menu包含两种,一种是直接对应Action的,一种是父菜单, ...