118. Pascal's Triangle

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

一刷

public class Solution {
    public List> generate(int numRows) {
        List> res = new ArrayList<>();
        if(numRows<=0) return res;
        List list = new ArrayList<>();
        list.add(1);
        res.add(new ArrayList<>(list));
        for(int i=1; i();
            for(int j=0; j<=i; j++){
                if(j == 0 || j== i) list.add(1);
                else list.add(res.get(i-1).get(j-1) + res.get(i-1).get(j));
            }
            res.add(new ArrayList<>(list));
        }
        return res;  
    }
}

你可能感兴趣的:(118. Pascal's Triangle)