Share my C++ code


  • 0
    T
    class Solution {
    public:
        vector<vector<int>> generate(int numRows) {
            if (numRows==0)
                return {};
            vector<vector<int>>vec(numRows,vector<int>(1));
            vec[0][0]=1;
            for (int i=1;i<numRows;i++)
            {    
                vec[i].resize(i+1);
                vec[i][0]=vec[i][vec[i].size()-1]=1;
                for (int j=1;j<vec[i].size()-1;j++)
                    vec[i][j]=vec[i-1][j-1]+vec[i-1][j];
            }
            return vec;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.