1. 程式人生 > >LintCode 109. 數字三角形

LintCode 109. 數字三角形

個數 str write -s integer sts inf ota body

給定一個數字三角形,找到從頂部到底部的最小路徑和。每一步可以移動到下面一行的相鄰數字上。

樣例

比如,給出下列數字三角形:

[
     [2],
    [3,4],
   [6,5,7],
  [4,1,8,3]
]

從頂到底部的最小路徑和為11 ( 2 + 3 + 5 + 1 = 11)。

class Solution {
public:
    /*
     * @param triangle: a list of lists of integers
     * @return: An integer, minimum path sum
     */
    int minimumTotal(vector<vector<int
>> &triangle) { // write your code here int row=triangle.size(); for(row=row-2;row>=0;row--) { for(int col=0;col<=row;col++) { triangle[row][col]+=min(triangle[row+1][col],triangle[row+1][col+1]); } }
return triangle[0][0]; } };

LintCode 109. 數字三角形