Very simple C++ solution (0 ms)


  • 1
    D
    vector<vector<int>> generate(int numRows) {
        
        vector<vector<int>> grid(numRows);
        
        // 1. Create and initialize grid
        for(int i = 0; i  < grid.size(); i++){
            
            // Insert 0's
            for(int j = 0; j < i+1; j++) grid[i].push_back(0);
            
            // insert 1's
            grid[i][0] = 1; 
    
        }
    
        // 2. Perform pascal operation
        for(int i = 1; i < grid.size(); i++){
            
            int a,b;
            for(int j = 1; j <= i; j++){
                
                int a = grid[i-1][j-1];
                int b;
                
                if(j == i) b = 0;
                else b = grid[i-1][j];
                
                grid[i][j] = a + b;
                
            }
        }
        
        return grid;
        
    }

Log in to reply
 

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