代码实现:
int minimumTotal(int **triangle, int triangleSize, int *triangleColSize) { if (triangleSize == 1) { return triangle[0][0]; } for (int i = 1; i < triangleSize; i++) { for (int j = 0; j < triangleColSize[i]; j++) { int x = i - 1; int y1 = j - 1, y2 = j; int num = INT32_MAX; if (y1 >= 0) { num = triangle[x][y1]; } if (y2 < triangleColSize[i - 1]) { if (num > triangle[x][y2]) { num = triangle[x][y2]; } } triangle[i][j] += num; } } int i = triangleSize - 1; int res = triangle[i][0]; for (int j = 1; j < triangleColSize[triangleSize - 1]; j++) { if (res > triangle[i][j]) { res = triangle[i][j]; } } return res; }