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?
Subscribe to see which companies asked this question.
思路:
思路与Pascal's Triangle 一样 利用上一层的值改变当前层的值 只是遍历改变值由后往前遍历了 只是空间变为O(k)
C++ 代码:
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> res(rowIndex+1,1);
for(int i=1;i<rowIndex;i++){
for(int j=i;j>0;j--){
res[j] += res[j-1];
}
}
return res;
}
};