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) {
ArrayList<Integer> cur = new ArrayList<Integer>();
cur.add(1);
for (int i = 0; i < rowIndex; i++) {
for (int j = cur.size()-1; j >0; j--) {
cur.set(j, cur.get(j)+cur.get(j-1));
}
cur.add(1);
}
return cur;
}