118. Pascal's Triangle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> resultList = new ArrayList<>(numRows);
resultList.add(Collections.singletonList(1));

for (int i = 1; i < numRows; i++) {
List<Integer> list = new ArrayList<>(i + 1);
list.add(1);
for (int j = 1; j < i; j++) {
list.add(resultList.get(i - 1).get(j - 1) + resultList.get(i - 1).get(j));
}
list.add(1);
resultList.add(list);
}

return resultList;
}
}

References

118. Pascal’s Triangle