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 class Solution {
public List<Integer> getRow(int rowIndex) {
ArrayList<ArrayList<Integer>> rst = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> first = new ArrayList<Integer>();
if(rowIndex<0)
return first;
first.add(0, 1);
rst.add(first);
for (int i = 1; i < rowIndex+1; i++) {
ArrayList<Integer> tmp = new ArrayList<Integer>(i + 1);
for (int j = 0; j < i + 1; j++){
tmp.add(-1);
}
ArrayList<Integer> prev = rst.get(i - 1);
tmp.set(0, prev.get(0));
tmp.set(i, prev.get(i - 1));
for (int j = 1; j < i; j++){
tmp.set(j, prev.get(j - 1)+prev.get(j));
}
rst.add(tmp);
}
return rst.get(rowIndex);
}
}
173

被折叠的 条评论
为什么被折叠?



