118. Pascal's Triangle

Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.

In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 5
Output:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]
class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> res = new ArrayList<>();
        
        if(numRows == 0) return res;
       
        //
        List<Integer> list = new ArrayList<>();
        list.add(1);
        res.add(list);
        List<Integer> pre = list;
    
        for(int i = 1; i < numRows; i++){
            List<Integer> tmp = new ArrayList<>();
            
            for(int j = 0; j < pre.size()+1;j++){
                
                int val = (j == 0 || j == pre.size()) ? 1 : pre.get(j) + pre.get(j-1);
                tmp.add(j,val);
                
            }
            
            pre = tmp;
            
            res.add(tmp);
        }
        
        return res;
    }
}

Last updated