-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path最小路径和.cpp
70 lines (58 loc) · 1.27 KB
/
最小路径和.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#include <unordered_map>
#include <functional>
using namespace std;
/*
最小路径和
和前两题方法一样。
*/
class Solution
{
public:
int minPathSum(vector<vector<int>>& grid)
{
int m = grid.size(), n = grid[0].size();
vector<vector<int>> res(m, vector<int>(n, 0));
res[0][0] = grid[0][0];
//初始化
for (int i = 1; i < n; ++i)
res[0][i] = res[0][i - 1] + grid[0][i];
for (int i = 1; i < m; ++i)
res[i][0] = res[i - 1][0] + grid[i][0];
for (int i = 1; i < m; ++i)
{
for (int j = 1; j < n; ++j)
{
res[i][j] = grid[i][j] + min(res[i][j - 1], res[i - 1][j]);
}
}
return res[m - 1][n - 1];
}
//update 2019.10.26
int minPathSum(vector<vector<int>>& grid)
{
int m = grid.size(), n = grid[0].size();
for (int i = 1; i < n; ++i)
grid[0][i] += grid[0][i - 1];
for (int i = 1; i < m; ++i)
grid[i][0] += grid[i - 1][0];
for (int i = 1; i<m; ++i)
for (int j = 1; j < n; ++j)
grid[i][j] += min(grid[i - 1][j], grid[i][j - 1]);
return grid[m - 1][n - 1];
}
};
int main()
{
Solution a;
vector<vector<int>> grid{ {1,2,5} };
cout << a.minPathSum(grid) << endl;
system("pause");
return 0;
}