Pascal;s Triangle

mail@pastecode.io avatar
unknown
c_cpp
22 days ago
708 B
1
Indexable
Never
class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> triangle;
        if(numRows==1)
            return {{1}};
        if(numRows==2)
            return {{1},{1,1}};
        triangle.push_back({1});
        triangle.push_back({1,1});
        int tIndex=2;
        for(int i=3;i<=numRows;i++)
        {
            vector<int> row;
            row.push_back(1);
            for(int y=1;y<i-1;y++)
            {
                row.push_back(triangle[tIndex-1][y-1]+triangle[tIndex-1][y]);
            }
            row.push_back(1);
            triangle.push_back(row);
            ++tIndex;
        }
        return triangle;
    }
};
Leave a Comment