/**
* 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. VC与VB

    VB调用VC dll的返回方式 第一种类型:数值传递注意:在VB中,默认变量传递方式为ByRef为地址,而传递值就是用ByVal,还要注意在C++中,int类型的变量是32位的,在VB中要用long型 ...

  2. 抖音短视频爆火的背后到底是什么——如何快速的开发一个完整的直播app

    前言 今年移动直播行业的兴起,诞生了一大批网红,甚至明星也开始直播了,因此不得不跟上时代的步伐,由于第一次接触的原因,因此花了很多时间了解直播,今天我来教你从零开始搭建一个完整的直播app,希望能帮助 ...

  3. 帆软用工具测试超链接打开弹窗(iframe嵌套),解决js传参带中文传递有乱码问题

    1.新建超链接 随意点击一个单元格右击,选择 超级链接 2.在弹出的窗口中选择JavaScript脚本 如图: 其中红框框出的是几个要点   ,左边的就不讲了,右上角的参数cc是设置了公式remote ...

  4. (十八)面向流水线的设计:CPU的一心多用

    一.单指令周期       由前可知,一条CPU指令的执行有三个步骤:指令读取.指令译码.指令执行.由于这个过程受CPU时钟的控制,如果我们将这个过程安排在一个CPU时钟周期内执行,这种设计思路就叫单 ...

  5. mysql-installer-web-community和mysql-installer-communityl两个版本的区别

    mysql的官网上提供了两个mysql共享版本的安装,一个20M,一个373.4M.二者的区别是前者是联网安装,当安装时必须能访问互联网,后者是离线安装使用的,一般建议下载离线安装使用的版本. 学Py ...

  6. 超详细讲解mysql存储过程中的in/out/inout

    存储过程 大概定义:用一个别名来描述多个sql语句的执行过程. 最简单 delimiter // create PROCEDURE p1() begin select * from userinfo; ...

  7. CTF流量分析题大全(掘安攻防平台)

    突然想做一下流量分析题,记得掘安攻防实验室上面有很多的流量分析题目,故做之 流量分析题一般使用的都是wireshark,(流量分析工具中的王牌 夺取阿富汗 说了分析http头,所以直接过滤http协议 ...

  8. pyinstaller---将py文件打包成exe

    pyinstaller可将Python脚本打包成可执行程序,使在没有Python环境的机器上运行. 1.pyinstaller在windows下的安装 直接在命令行用pip安装 pyinstaller ...

  9. Elements-of-Python_03_LanguageFeature

    (内容包括Python语法概述,流程控制,条件表达式) 1 Python语法 1.1 Python的特点 Python是一种完全面向对象的.解释性的.可移植的.开源的脚本编程高级语言,具有丰富的库,允 ...

  10. 当你在浏览器输入一个网址(如http://www.taobao.com),按回车之后发生了什么?

    首先你输入了一个网址并按下了回车,这个时候浏览器会根据这个URL去查找其对应的IP,具体过程如下: 首先是查找浏览器缓存,浏览器会保存一段时间你之前访问过的一些网址的DNS信息,不同浏览器保存的时常不 ...