Pascal's Triangle

来源:互联网 发布:python 贝叶斯分类器 编辑:程序博客网 时间:2024/06/12 16:51

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]]
class Solution {public:    vector<vector<int> > generate(int numRows)    {        vector<vector<int> >res;        for (int row = 0; row < numRows; row++)        {            vector<int> temp;            for (int col = 0; col <= row; col++)            {                if (0 == col || row == col)                    temp.push_back(1);                else                    temp.push_back(res[row-1][col-1] + res[row-1][col]);            }            res.push_back(temp);        }        return res;    }};



0 0
原创粉丝点击