[Leetcode]#118 Pascal's Triangle

来源:互联网 发布:单片机软件工程师岗位 编辑:程序博客网 时间:2024/05/21 10:25

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

//#118 Pascal's Triangle//0ms 100%class Solution {public:    vector< vector<int> > generate(int numRows)     {        vector< vector<int> > v_v;        if(numRows == 0) return v_v;        vector<int> v0(1, 1);        v_v.push_back(v0);        for(int i=1; i<numRows; i++)        {            vector<int> v(v_v[i-1].size()+1, 1);            for(unsigned int j=1; j<v.size()-1; j++)            {                v[j] = v_v[i-1][j-1] + v_v[i-1][j];            }            v_v.push_back(v);                   }        return v_v;    }};
0 0
原创粉丝点击