forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
30 lines (26 loc) · 915 Bytes
/
Solution.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
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
size_t rowNum = triangle.size();
//特殊值处理
if(rowNum == 0)return 0;
if(rowNum == 1){
if(triangle[0].empty())return 0;
else return triangle[0][0];
}
for(int i = 1;i<rowNum;i++){
for(int j = i;j>=0;j--){
//边界处理
if(j == 0){triangle[i][j] = triangle[i][j] + triangle[i-1][j];continue;}
if(j == i){triangle[i][j] = triangle[i][j] + triangle[i-1][j-1];continue;}
//一般处理
triangle[i][j] = triangle[i][j] + min(triangle[i-1][j],triangle[i-1][j-1]);
}
}
int ans = INT_MAX;
for(auto v : triangle[rowNum-1]){
if(ans > v)ans = v;
}
return ans;
}
};