题目

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. [ 转载 ] Http详解

    HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...

  2. faker php测试数据库生成

    官方地址:https://github.com/fzaninotto/Faker 使用方式: 1.composer直接下载: composer require fzaninotto/faker 2.将 ...

  3. 【学习笔记】python2和python3的input()

    python2中的input()只接受变量作为传入值,非变量内容会报错. >>> user=input("Enter your name:") Enter you ...

  4. 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem K. KMC Attacks 交互题 暴力

    Problem K. KMC Attacks 题目连接: http://codeforces.com/gym/100714 Description Warrant VI is a remote pla ...

  5. 【转】SQL Server游标的使用

    在关系数据库中,我们对于查询的思考是面向集合的.而游标打破了这一规则,游标使得我们思考方式变为逐行进行.对于类C的开发人员来着,这样的思考方式会更加舒服. 正常面向集合的思维方式是: 而对于游标来说: ...

  6. ThreadLocal 详解

    什么是ThreadLocal 根据JDK文档中的解释:ThreadLocal的作用是提供线程内的局部变量,这种变量在多线程环境下访问时能够保证各个线程里变量的独立性. 从这里可以看出,引入Thread ...

  7. linux 下安装 RZ SZ命令 以及使用

    对于经常使用Linux系统的人员来说,少不了将本地的文件上传到服务器或者从服务器上下载文件到本地,rz / sz命令很方便的帮我们实现了这个功能,rz是把win的文件上传到linux上    sz是吧 ...

  8. vue项目开发之v-for列表渲染的坑

    不知道大家在用vue开发的过程中有没有遇到过在使用v-for的时候会出现大片的黄色警告,比如下图: 其实这是因为没有写key的原因 :key是为vue的响应式渲染提供方法,在列表中单条数据改变的情况下 ...

  9. CentOS下多网卡绑定多IP段时导致只有一个会通的问题解决

    原因:Linux默认开启了反向路由检查导致的,比如说外面访问eth0的网卡,而网关在eth1上,又或者从eth0出的流量,而网关在eth1上,此时会检查到网关不在同一个网卡上导致出不去,进不来的问题. ...

  10. Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable(转)

    最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考: Spring 注解学习手札(一 ...