[LeetCode]: 64: Minimum Path Sum
2015-10-14 17:45
537 查看
题目:
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.
思路:动态规划
整个序列的最小路径= 上一个节点的最小路径+当前路径的节点值
依次往前看,第一个点的最小序列等于其本身
代码:
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.length == 0){ return 0; } int[][] intResult = new int[grid.length][grid[0].length]; for(int i = 0;i<grid.length;i++){ for(int j = 0;j<grid[i].length;j++){ if(i==0 && j==0){ intResult[i][j] = grid[i][j]; }else if(i == 0 && j!=0){ intResult[i][j] = intResult[i][j-1] + grid[i][j]; } else if(j== 0 && i!= 0){ intResult[i][j] = intResult[i-1][j] + grid[i][j]; } else{ int intX = intResult[i][j-1] + grid[i][j]; int intY = intResult[i-1][j] + grid[i][j]; if(intX > intY){ intResult[i][j] = intY; } else{ intResult[i][j] = intX; } } } } return intResult[grid.length-1][grid[0].length-1]; }
相关文章推荐
- 学习一份百度的项目目录结构规范
- 2小时入门Robot Framework
- hdoj--1864--最大保险额(背包)
- 第四周项目3-单链表的应用(1)单链表算法
- HDU DP
- apk无法打包
- Android ListView, GridView中android:drawSelectorOnTop含义
- UITableView刷新数据reLoadData
- ASP.NET通过http/https的POST方式,发送和接受XML文件内容(转)
- c++ 内存泄露的检查
- Druid、C3P0、Tomcat Pool的性能测试与选型
- 【C++】预编译头文件
- 二叉树的操作
- Android--内存管理机制
- Android DropBoxManager Service
- 日记 - Mysql数据库的基础知识一
- <LeetCode><Easy>231 Power of Two
- python中怎样获取文件的修改时间及其他系统信息状态-os.stat()
- 明明的随机数
- 再谈算法复杂度