119. Pascal's Triangle II

Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal’s triangle.

In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:

根据杨辉三角形的规则递归。
每次递归行数-1。
根据上一行的返回值,生成新行的列表,然后返回。
如果生成行数为0则返回{1}。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public List<Integer> getRow(int rowIndex) {
if(rowIndex == 0){
List<Integer> ret = new ArrayList();
ret.add(1);
return ret;
}

List<Integer> arr = getRow(rowIndex - 1);
List<Integer> ret = new ArrayList();

for(int i = 0; i < arr.size()+1; i++){
if(i == 0 || i == arr.size()){
ret.add(1);
}
else{
ret.add(arr.get(i) + arr.get(i-1));
}
}
return ret;
}
}
Author

Xander

Posted on

2022-04-18

Updated on

2022-04-18

Licensed under

Comments