版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sqh201030412/article/details/78679811
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?
给定一个索引k,返回帕斯卡三角形的第k行。 例如,给定k=3,
仅仅使用O(k)额外空间.
//从后向前
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;
}