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?
这道题和118有点类似,但是很取巧,用一个list做出来,每次迭代的时候用原先list的数据,由于会被抹去,所以从后面开始操作
public class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> res = new ArrayList<Integer>();
res.add(1);
if(rowIndex == 0) return res;
for(int i=1;i<=rowIndex;i++){
for(int j=i-1;j>0;j--){
res.set(j,res.get(j-1)+res.get(j));
}
res.add(1);
}
return res;
}
}
微信扫一扫
支付宝扫一扫