Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.
Note that the row index starts from 0.
/* 输出杨辉三角
* 1 1
* 1 1 1 1
* 1 2 1 1 2 1 显然只用计算对角线左下部分 即j<i部分
*1 3 3 1 1 3 3 1 滚动数组优化 循环row-1次 每次循环时 a[i] += a[i-1]
* 1 4 6 4 1
*
* */
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ret(rowIndex+1, 1);
for(int i=2;i<=rowIndex;i++){
for(int j=i-1;j>=1;j--){//先计算右边的值 保证左边的值还没被覆盖 反方向覆盖 这里和01背包中一维数组优化类似的原理
ret[j] += ret[j-1];
}
}
return ret;
}
};