Minimum Path Sum
来源:互联网 发布:国密sm4算法 编辑:程序博客网 时间:2024/06/08 03:09
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.
class Solution {public:int rows,cols; int minPathSum(vector<vector<int> > &grid) { if(!grid.size() || !grid[0].size())return 0;rows = grid.size();cols = grid[0].size();vector<vector<int> >dp;vector<int> v;for(int i = 0; i < rows; i++){v.clear();//vector二维初始化,不要像数组那样直接dp[i][j] = xx,越界,习惯!for(int j = 0; j < cols; j++)v.push_back(-1);dp.push_back(v);}return minSum(0, 0, dp, grid); }int minSum(int r, int c, vector<vector<int> > &dp,vector<vector<int> > &grid){if(r == rows - 1 && c == cols - 1){dp[r][c] = grid[r][c];return dp[r][c];}if(dp[r][c] != -1)return dp[r][c];int rst = INT_MAX;if(c < cols - 1)rst = grid[r][c] + minSum(r, c + 1, dp, grid);if(r < rows - 1)rst = min(rst, grid[r][c] + minSum(r + 1, c, dp, grid));dp[r][c] = rst;return rst;}};
0 0
- LeetCode: Minimum Path Sum
- LeetCode Minimum Path Sum
- LeetCode : Minimum Path Sum
- [Leetcode] Minimum Path Sum
- Minimum Path Sum
- Minimum Path Sum
- [LeetCode] Minimum Path Sum
- [Leetcode] Minimum Path Sum
- Minimum Path Sum
- [LeetCode]Minimum Path Sum
- Minimum Path Sum
- Minimum Path Sum
- Minimum Path Sum
- Minimum Path Sum
- [leetcode]Minimum Path Sum
- Minimum Path Sum
- LeetCode-Minimum Path Sum
- [leetcode] Minimum Path Sum
- php对象转成数组和数组转成对象操作
- Android开发神器(里面有各种UI特效和android代码库实例)
- nginx反向代理,动静态请求分离,以及nginx缓存应用,以及使用ngx_cache_purge清除指定URL
- iOS 图片命名规则
- wegwehg
- Minimum Path Sum
- 如何在 Java 上使用 JAX-RS (RESTful 风格的 Web Services)
- 回调函数
- Ubuntu使用crontab定时任务
- Star and Matrix
- svn 错误解决
- Android下Sqlite数据库ORM框架之GreenDao详解
- TCP 的那些事儿(上)
- 输出字符串数组