原题:
Given an index k, return the kth row of the Pascal’s triangle.
For example, given k = 3,
Return [1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
思路:此题是计算杨辉三角的题,比较简单,只要知道杨辉三角是怎么计算来的就可以写出
public List<Integer> getRow(int rowIndex) {
List<Integer> list = new ArrayList<>();
if (rowIndex < 0)
return list;
list.add(1);
if (rowIndex == 0)
return list;
for (int i = 1; i <= rowIndex ; i++) {
for (int j = list.size()-1; j > 0; j--) {
list.set(j,list.get(j-1)+list.get(j));
}
list.add(1);
}
return list;
}