/**
* 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. JZOJ 2020.10.7 提高B组反思

    JZOJ 2020.10.7 提高B组反思 T1 比较简单的一道题 跑\(k\)遍\(SPFA\) 然后全排列顺序枚举求解 TLE 60 双向存边数组没开两倍-- T2 搞出分母 分子不会求 \(n^ ...

  2. python模块wifi使用小记

    安装命令 pip install wifi 连接命令 sudo wifi connect --add-hoc ssid,使用该命令会修改/etc/network/interfaces配置文件,导致启动 ...

  3. 微服务注册到Nacos的IP私网172.x.x.x网段无法访问的问题

    解决方案一 显示声明注册服务实例的外网IP,默认就是使用私网的IP造成无法访问的,配置如下: spring: cloud: nacos: discovery: ip: 101.37.6.8 解决方案二 ...

  4. [BJDCTF2020]Mark loves cat && [BJDCTF 2nd]简单注入 && [BJDCTF2020]The mystery of ip

    [BJDCTF2020]Mark loves cat 源码泄露 使用GitHack.py下载源码 下载之后对源代码进行审计 flag.php代码为: <?php $flag = file_get ...

  5. 【软件测试部署基础】npm的认识

    1. npm简介 先来看下官方介绍: npm makes it easy for JavaScript developers to share and reuse code, and it makes ...

  6. 转载 HTTP协议

    转载自:http://www.cnblogs.com/TankXiao/archive/2012/02/13/2342672.html 当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, ...

  7. SpringCloud-服务间通信方式

    接下来在整个微服务架构中,我们比较关心的就是服务间的服务改如何调用,有哪些调用方式? 总结:在springcloud中服务间调用方式主要是使用 http restful方式进行服务间调用 1. 基于R ...

  8. js内存泄漏的问题?

    内存泄漏指任何对象在您不再拥有或需要它之后仍然存在. 垃圾回收器定期扫描对象,并计算引用了每个对象的其他对象的数量.如果一个对象的引用数量为 0(没有其他对象引用过该对象),或对该对象的惟一引用是循环 ...

  9. springboot:定时任务

    在日常的开发过程中经常使用到定时任务,在springMVC的开发中,经常和quartz框架进行集成使用,但在springboot中没有这么做,而是使用了java的线程池来实现定时任务. 一.概述 在s ...

  10. 深入理解Java虚拟机(一)——JVM内存模型

    文章目录 程序计数器 定义 作用 特点 Java虚拟机栈 定义 特点 本地方法栈 定义 Java堆 定义 特点 方法区 定义 特点 运行常量池 直接内存 总结 Java虚拟机的内存空间分为五个部分: ...