给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。
在杨辉三角中,每个数是它左上方和右上方的数的和。
示例:
输入: 3
输出: [1,3,3,1]
来源:力扣(LeetCode)
class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> lists = new ArrayList<>();
lists.add(1);
if(rowIndex == 0) {
return lists;
}
lists.add(1);
if(rowIndex == 1) {
return lists;
}
List<Integer>list = new ArrayList<>();
list.add(1);
for(int i = 0; i <= rowIndex; i ++ ) {
lists = list;
list = new ArrayList<>();
for(int j = 0 ; j <= i ;j ++ ) {
if(j == 0|| j == i) {
list.add(1);
}else {
int a = lists.get(j-1) + lists.get(j);
list.add(a);
}
}
}
return list;
}
}