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<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(new ArrayList<>(list));
for(int i=1; i<numRows; i++){
list = new ArrayList<>();
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;
}
}