My C++ code for reference


  • 5
    J
    class Solution {
    public:
        vector<vector<int> > generate(int numRows) {
            
            if(numRows==0) return vector<vector<int> >();
            
            vector<vector<int> > res;
            
            res.resize(numRows);
            
            res[0].push_back(1);
            
            if(numRows==1) return res;
            
            for(int i=1; i<numRows; i++){
                
                res[i].resize(i+1); // make vector for each row 
                
                res[i][0]=res[i][i]=1; // put 1 to first and last element
                
                for(int j=1; j<i; j++) 
                    res[i][j]=res[i-1][j]+res[i-1][j-1]; // generate other elements of the row
                
            }
            
            return res;
            
        }
    };

Log in to reply
 

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