标签:
1 class Solution { 2 public: 3 int minimumTotal(vector<vector<int> > &triangle) { 4 vector<vector<int>> temp(triangle); 5 vector<vector<int>>::size_type length=temp.size(); 6 int i,j; 7 for(i=1;i<length;i++){ 8 vector<int>::size_type length_inner = temp[i].size(); 9 for(j=0;j<length_inner;j++){ 10 if(j == 0){ 11 temp[i][j] = temp[i][j] + temp[i-1][j]; 12 }else if(j == length_inner - 1){ 13 temp[i][j] = temp[i][j] + temp[i-1][j-1]; 14 }else{ 15 temp[i][j] = (temp[i][j] + temp[i-1][j-1] < temp[i][j] + temp[i-1][j] ? temp[i][j] + temp[i-1][j-1]:temp[i][j] + temp[i-1][j]); 16 } 17 } 18 } 19 int min = temp[length-1][0]; 20 for(i=1;i<temp[length-1].size();i++){ 21 min = (min < temp[length-1][i]?min:temp[length-1][i]); 22 } 23 return min; 24 25 } 26 };
标签:
原文地址:http://www.cnblogs.com/irun/p/4506878.html