寻找矩阵中最短路径 Minimum Path Sum
来源:互联网 发布:js数组去重函数 编辑:程序博客网 时间:2024/06/05 20:58
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.
算法:动态规划法,因为只能向下或者右行走,所以从[1,1]位置开始,当前最小路径由上方与左方即[0,1],[1,0]最小值决定。
代码
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
if(grid.size()<1)
return 0;
unsigned long m = grid.size();
unsigned long n = grid[0].size();
for (int i = 1; i < m; ++i) {
grid[i][0] = grid[i-1][0] + grid[i][0];
}
for (int j = 1; j < n; ++j) {
grid[0][j] = grid[0][j-1] + grid[0][j];
}
for (int k = 1; k < m; ++k) {
for (int l = 1; l < n; ++l) {
grid[k][l] = min(grid[k-1][l],grid[k][l-1]) + grid[k][l];
}
}
return grid[m-1][n-1];
}
};
0 0
- 寻找矩阵中最短路径 Minimum Path Sum
- 矩阵上寻找最短路径 Minimum Path Sum
- Minimum Path Sum 矩阵最短路径和@LeetCode
- Minimum Path Sum 最小路径和
- [leetcode]Minimum Path Sum 最小路径和
- Leetcode:Minimum Path Sum 最小路径和
- LinCode Minimum Path Sum 最小路径和
- leetcode64-Minimum Path Sum(最小路径和)
- 64. Minimum Path Sum 路径最小总和
- Minimum Path Sum 最小值的路径
- lintcode minimum-path-sum 最小路径和
- 最小路径和—Minimum Path Sum
- LeetCode(Minimum Path Sum) 找到路径中和最小的
- LeetCode | Minimum Path Sum(最小路径和)
- LeetCode OJ 之 Minimum Path Sum (最小路径和)
- 动态规划--最小路径和(Minimum Path Sum)
- Leetcode 64. Minimum Path Sum 最小路径和 解题报告
- LeetCode 64. Minimum Path Sum(最小路径和)
- uniquePaths
- 数组全排列
- 2016年04月10日
- vector api
- 哈希表与红黑树
- 寻找矩阵中最短路径 Minimum Path Sum
- generateMatrix 给定N 返回指定矩阵
- 判断幂数
- Different Ways to Add Parentheses
- binary tree ab
- URL编码的简单理解
- 特征选择专题
- linux chmod 命令
- 算法评判标准:召回率(recall)与查准率(precision)