Posted on

Description

Submission

class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        if(numRows == 0) return {};
        vector<vector<int>> res;
        res.push_back({1});
        if(numRows == 1) return res;
        res.push_back({1, 1});
        if(numRows == 2) return res;

        for(int i = 3; i <= numRows; ++i) {
            vector<int> row;
            vector<int>& lastRow = res.back();
            row.push_back(lastRow[0]);
            for(int i = 0; i < lastRow.size() - 1; ++i) {
                row.push_back(lastRow[i] + lastRow[i+1]);
            }
            row.push_back(lastRow.back());
            res.push_back(row);
        }
        return res;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *