leetcode [#119]

目录

题目

Given an index k, return the kth row of the Pascal’s triangle.

Example:
given k = 3,
Return [1,3,3,1].


解决方案

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

List<Integer> row = new ArrayList<>();
List<List<Integer>> result = new ArrayList<List<Integer>>();

for(int i = 1; i <= rowIndex + 1; i++){
List<Integer> curr = new ArrayList<Integer>();
curr.add(1);
for(int j = 1; j < (i-1); j++){
curr.add(result.get(i-2).get(j-1) + result.get(i-2).get(j));
}
if(i > 1){
curr.add(1);
}
result.add(curr);
}
return result.get(result.size()-1);
}
}

注意事项

  1. 在118题的基础上,本题只需要构建相应层数的杨辉三角,并返回最后一层的元素数组即可。