LeetCode: Triangle
基本一次过
1 class Solution { 2 public: 3 int minimumTotal(vector<vector<int> > &triangle) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if (!triangle.size()) return 0; 7 if (triangle.size() == 1) return triangle[0][0]; 8 vector<int> pre = triangle[0]; 9 for (int i = 1; i < triangle.size(); i++) { 10 vector<int> tmp = pre; 11 for (int j = 0; j < pre.size(); j++) { 12 if (!j) tmp[j] = pre[j] + triangle[i][j]; 13 else tmp[j] = min(pre[j-1], pre[j]) + triangle[i][j]; 14 } 15 tmp.push_back(triangle[i][i]+pre[pre.size()-1]); 16 pre = tmp; 17 } 18 int ret = pre[0]; 19 for (int i = 1; i < pre.size(); i++) ret = min(ret, pre[i]); 20 return ret; 21 } 22 };