-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path120.三角形最小路径和.cpp
More file actions
52 lines (49 loc) · 1.3 KB
/
120.三角形最小路径和.cpp
File metadata and controls
52 lines (49 loc) · 1.3 KB
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
/*
* @lc app=leetcode.cn id=120 lang=cpp
*
* [120] 三角形最小路径和
*/
// @lc code=start
class Solution {
public:
// 只考虑左方影响时。
int minimumTotal(vector<vector<int>>& triangle) {
int result;
vector<int> cache;
for (int i = 0; i < triangle.size(); i++)
{
cache.push_back(0);
for (int j = i; j >= 0; j--)
{
if (j != 0)
{
if (j == i)
{
cache.at(j) = cache.at(j - 1);
}
else if (cache.at(j - 1) < cache.at(j))
{
cache.at(j) = cache.at(j - 1);
}
}
cache.at(j) += triangle.at(i).at(j);
if (i == triangle.size() - 1)
{
if (j == i)
{
result = cache.at(j);
}
else
{
if (cache.at(j) < result)
{
result = cache.at(j);
}
}
}
}
}
return result;
}
};
// @lc code=end