Sharing my C++ solution


  • 0
    T
    class Solution {
    public:
        vector<vector<int>> generate(int numRows) {
            vector<vector<int>> result(0);
            if(numRows<=0)
                return result;
                
            result.resize(numRows);
            result[0].push_back(1);
            
            for(int n=2; n<=numRows; n++)
            {
                result[n-1].resize(n);
                result[n-1][0] = 1;
                result[n-1][n-1] = 1;
                for(int i=1; i<n-1; i++)
                    result[n-1][i] = result[n-2][i-1] + result[n-2][i];
            }
            
            return result;
        }
    };

Log in to reply
 

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