LeetCode题解——Minimum Path Sum
2015-11-18 20:36
375 查看
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.
解题思路:动态规划,p[i][j]表示从grid[0][0]到grid[i][j]的最小距离。p[i][j] = min(p[i-1][j] , p[i][j-1]) + grid[i][j]
Note: You can only move either down or right at any point in time.
解题思路:动态规划,p[i][j]表示从grid[0][0]到grid[i][j]的最小距离。p[i][j] = min(p[i-1][j] , p[i][j-1]) + grid[i][j]
class Solution { public: int minPathSum(vector<vector<int>>& grid) { if(!grid.size()) return 0; int m = grid.size(), n = grid[0].size(); int p[m] ; p[0][0] = grid[0][0]; for(int i=1 ;i<m;i++){ p[i][0]=p[i-1][0]+grid[i][0]; } for(int j=1; j<n;j++){ p[0][j]=p[0][j-1]+grid[0][j]; } for(int i=1; i<m; i++){ for(int j=1; j<n; j++){ p[i][j] = min(p[i][j-1],p[i-1][j])+grid[i][j]; } } return p[m-1][n-1]; } };
相关文章推荐
- EIGHT 八段码
- shell入门-grep2
- 跨域的jsonP
- 慕课网 3.1 异常处理
- LeetCode---Implement strStr()
- 关于listview的控件引用.SimpleAdapter
- 浅谈Android四大组件的简单用法
- nyoj--37--回文字符串(动态规划)
- linux/centos6.5下编译安装python2.7和python3.5.2
- hiho 1249 Xiongnu's Land
- linux/centos6.5下编译安装python2.7和python3.5.2
- 为Gradle添加UTF-8支持
- poj_2488 A Knight's Journey
- #在蓝懿学习iOS的日子#day21
- nyoj--37--回文字符串(动态规划)
- Fourteenth scrum meeting
- android104 帧动画,补间动画,属性动画
- websocket 配置
- Activity启动模式完全理解:standard, singleTop, singleTask 以及 singleInstance
- 如何为自定义相机添加 取景框|扫描框 |预览框|矩形框