[leetcode] 120. Triangle(Medium)

it2025-11-16  2

原题 思路: dp,从下往上依次取得最小的,取到最上面的,就是一条最小的路径。

class Solution { public: int minimumTotal(vector<vector<int>> &triangle) { int rowSize = triangle.size(); int colSize; if (rowSize == 0) return -1; if (rowSize == 1) return triangle[0][0]; for (int row = rowSize - 2; row >= 0; row--) { colSize = triangle[row].size(); for (int col = 0; col < colSize; col++) { triangle[row][col] += min(triangle[row + 1][col], triangle[row + 1][col + 1]); } } return triangle[0][0]; } };

转载于:https://www.cnblogs.com/ruoh3kou/p/9893421.html

相关资源:数据结构—成绩单生成器
最新回复(0)