[leetcode-64]Minimum Path Sum(C)
2015-08-04 10:00
447 查看
问题描述:
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.
分析:和上两道题一样,这个也是使用DP算法,比较简单。
代码如下:20ms
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.
分析:和上两道题一样,这个也是使用DP算法,比较简单。
代码如下:20ms
[code]int minPathSum(int** grid, int gridRowSize, int gridColSize) { int row,col; int *nums = (int *)malloc(sizeof(int)*gridColSize); row = gridRowSize-1; col = gridColSize-1; nums[col] = grid[row][col]; for(--col;col>=0;col--){ nums[col] = grid[row][col] + nums[col+1]; } for(row = gridRowSize-2;row>=0;row--){ for(col = gridColSize-1;col>=0;col--){ int right = 10000000; if(col+1<gridColSize) right = nums[col+1]; int offset = (right<nums[col])?right:nums[col]; nums[col] = grid[row][col]+offset; } } return nums[0]; }
相关文章推荐
- 关于下载Spring官方包
- Mac/WIN zip压缩包乱码问题解决
- STL之traits编程技法
- php安全配置分享
- Tomcat服务器学习和使用(一)
- 南邮 OJ 1056 地道
- angularJS--多个控制器之间的数据共享
- 稳定排序和不稳定排序
- HDU 1698 线段树成段更新模板
- 推荐chm转成html的方法
- java编译中出现了Exception in thread “main" java.lang.UnsupportedClassVersionError
- 【bzoj3620】 似乎在梦中见过的样子 KMP
- 杭电 1969 Pie 二分法 附翻译 解题思路
- Oracle视图查询慢之统计信息收集
- systemparametersinfo详细
- sizeof 计算类大小时需注意的几点
- floor(x) 函数
- ConCurrent in Practice小记 (3)
- 使用MyEclipse 9.0 创建 struts2 的HelloWorld 工程
- 布局1