MinPathSum(leetcode)
来源:互联网 发布:jamp软件的作用 编辑:程序博客网 时间:2024/06/05 23:01
题目:
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 int minPathSum(int[][] grid) { if(grid==null||grid.length==0||grid[0].length==0) { return 0; } int [][]dp=new int [grid.length][grid[0].length]; dp[0][0]=grid[0][0]; for(int i=1;i<grid.length;i++) { dp[i][0]=grid[i][0]+dp[i-1][0]; } for(int i=1;i<grid[0].length;i++) { dp[0][i]=grid[0][i]+dp[0][i-1]; } for(int i=1;i<grid.length;i++) { for(int j=1;j<grid[0].length;j++) { if(dp[i-1][j]>dp[i][j-1]) { dp[i][j]=dp[i][j-1]+grid[i][j]; }else { dp[i][j]=dp[i-1][j]+grid[i][j]; } } } return dp[grid.length-1][grid[0].length-1]; }
阅读全文
0 0
- MinPathSum(leetcode)
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- Painter's Problem POJ1681 高斯消元
- 设计模式学习笔记
- sdut-离散题目5
- XML
- linux下的CSV文件操作
- MinPathSum(leetcode)
- 9. Palindrome Number
- pycharm注册
- sdut-离散题目6
- AMD 和 CMD 的区别有哪些?
- oracle 与 SQL server区别 实例 数据库 表空间
- redis持久化RDB和AOF
- 乐享其约CEO施建锋:微信预约时代的到来
- Spring_SSH整合之_架构的历史(四层架构整合JSP+MySQL+JavaBean+Hibernate+Dao+Impl)_4