题目

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.

题解

这道题跟Unique Path系列是思路一样的。具体思路看代码就很清楚了

代码如下:

 1     public int minPathSum(int[][] grid) {
 2         int m = grid.length;
 3         int n = grid[0].length;
 4         
 5         if(m==0||n==0)
 6             return 0;
 7             
 8         int[][] dp = new int[m][n];
 9         
         dp[0][0]=grid[0][0];
         
         //a row
         for (int i = 1; i < n; i++) 
             dp[0][i] = dp[0][i - 1] + grid[0][i];  
 
         //a column
         for (int j = 1; j < m; j++)   
             dp[j][0] = dp[j - 1][0] + grid[j][0];  
         
         for (int i=1; i<m; i++){  
                 for (int j=1; j<n; j++){  
                     if(dp[i-1][j]<dp[i][j-1])
                         dp[i][j]=dp[i-1][j]+grid[i][j];
                     else
                         dp[i][j]=dp[i][j-1]+grid[i][j];
                 }  
             }  
             return dp[m-1][n-1];  
     }

Minimum Path Sum leetcode java的更多相关文章

  1. 【LeetCode】Path Sum ---------LeetCode java 小结

    Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  2. Binary Tree Maximum Path Sum leetcode java

    题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...

  3. Minimum Path Sum [LeetCode]

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  4. Minimum Path Sum——LeetCode

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  5. Path Sum leetcode java

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  6. LeetCode 64. 最小路径和(Minimum Path Sum) 20

    64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...

  7. LeetCode: Minimum Path Sum 解题报告

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  8. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

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

随机推荐

  1. Gunicorn设计部分的翻译

    Design 关于Gunicorn架构的简要描述. Server Model Gunicorn是基于pre-fork(预启动,提前fork)的工作模式.这就意味着Gunicorn是由一个主进程来管理这 ...

  2. hdu 5774 Where Amazing Happens 水题

    Where Amazing Happens 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5774 Description As the premie ...

  3. Slickflow.NET 开源工作流引擎高级开发(二) -- 流程快速测试增值服务工具介绍

    前言:流程是由若干个任务节点组成,流转过程就是从一个节点转移到下一个节点,通常需要不断切换用户身份来完成流程的测试,这样使得测试效率比较低下,本文从实战出发,介绍常见的两种快速测试方法,用于提升流程测 ...

  4. Fiddler_解决Fiddler查看Post参数中文乱码的问题

    解决Fiddler查看Post参数中文乱码的问题 今天一个同事问我,为什么用Fiddler查看Post的中文参数,是一堆乱码,如下: 需要在注册表中增加一个键值: HKEY_CURRENT_USER\ ...

  5. HDU 4759 Poker Shuffle(2013长春网络赛1001题)

    Poker Shuffle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. hdu4467 Graph

    Graph Problem Description P. T. Tigris is a student currently studying graph theory. One day, when h ...

  7. Mustache.js语法

    看了Mustache的github,学学此中的语法,做个笔记 1.简单的变量调换:{{name}} 1 var data = { "name": "Willy" ...

  8. mormot中间件成功匹配客户端FDMemTable和ClientDataSet

    mormot中间件成功匹配客户端FDMemTable和ClientDataSet

  9. Linux 用户和用户操作

    1,创建组 groupadd test 增加一个test组 2,修改组 groupmod -n test2 test 将test组的名子改成test2 3,删除组 groupdel test2 删除  ...

  10. Android Initializing a Build Environment

    from://https://source.android.com/source/initializing.html#next-download-the-source Initializing a B ...