A clear cpp 0ms solution


  • 0
    M

    vector<vector<int>> generate(int numRows) {
    if(numRows==0)
    return vector<vector<int>>();
    vector<vector<int>>pascal(numRows);

        for(int i = 0 ; i < numRows;++i){
            pascal[i].resize(i+1);
            
        }
       
       
        for(int i = 0;i<numRows;++i){
            for(int j = 0 ; j <=i;++j)
            {
                if(j==0||j==i)     //at the edge of the triangle,it's must be 1
                    pascal[i][j]=1;
                else{
                    pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j]; 
                                                                                      //1
                                                                                      //11
                                                                                      //121  
                                                                                      //1331
                                                                                      //14641
                }
                
            }
            
        }
        
        
        return pascal;
    }

Log in to reply
 

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