Pascal's Triangle

来源:互联网 发布:淘宝网天猫男装外套 编辑:程序博客网 时间:2024/05/16 18:50

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]]
sol:

class Solution {public:    vector<vector<int> > generate(int numRows) {        // Start typing your C/C++ solution below        // DO NOT write int main() function                vector<vector<int>> res(numRows,vector<int>(1,1));        if(numRows <= 1) return res;                res[1].push_back(1);                for(int i = 2;i < numRows;i++)        {            for(int j = 1; j < i;j++)            {                res[i].push_back(res[i-1][j-1] +res[i-1][j]);            }            res[i].push_back(1);        }                return res;    }};

20 milli secs.



原创粉丝点击