120. Triangle

  • Given a triangle, find the minimum path sum from top to bottom. Each
    step you may move to adjacent numbers on the row below.

    For example, given the following triangle

class Solution {
public:
    int minimumTotal(vector<vector<int>>& triangle) {
        vector<int> minium;
        vector<int> temp;
        int min;

        minium.push_back(triangle[0][0]);
        for(int i = 1;ifor(int j = 0;jcout<if( j == 0){
                    temp.push_back(minium[j] + triangle[i][j]);
                }else if( j == triangle[i].size() -1){
                    temp.push_back(minium[j-1] + triangle[i][j]);
                }else{
                    temp.push_back((minium[j-1]1]:minium[j]) + triangle[i][j]); 
                }
            }
            minium.clear();
            minium.assign(temp.begin(), temp.end());
        }

        min = minium[0];
        for(int i = 0;ireturn min;
    }
};

你可能感兴趣的:(leetcode)