[leetcode-118-Pascal's Triangle]

Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

思路:

很明显,每一层第一个数字和最后一个数字均为1,中间其余数字只与上一层中的两个数字有关系。

即level[i][j] = level[i-1][j-1] + level[i-1][j](i>0 && j>0 && j<level.size)。

vector<vector<int> > generate(int numRows)
{
        vector<vector<int>> result;
        if(numRows<1)return result;
        vector<int>level;
        level.push_back(1);
        result.push_back(level);
        for(int i = 1;i<numRows;i++)
        {
            level.clear();
            level.push_back(1);
            for(int j=1;j<i;j++)
            {
                level.push_back(result[i-1][j-1] + result[i-1][j]);
            }
            level.push_back(1);
            result.push_back(level);
        }
        return result;
}
原文地址:https://www.cnblogs.com/hellowooorld/p/6666999.html