[leetcode][array] Pascal's Triangle

来源:互联网 发布:个人网络招商 编辑:程序博客网 时间:2024/06/07 14:57

题目:

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;        if(numRows <= 0) return res;        vector<int> oneRow;        oneRow.push_back(1);        res.push_back(oneRow);        oneRow.clear();        for(int i = 1; i < numRows; ++i){            for(int j = 0; j <= i; ++j){                if(0 == j || i == j) oneRow.push_back(1);                else oneRow.push_back(res[i-1][j-1]+res[i-1][j]);            }            res.push_back(oneRow);            oneRow.clear();        }        return res;    }};


0 0
原创粉丝点击