[Array]Pascal's Triangle II
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?
方法:在每一行的更新中,从后往前进行更新可以使代码更加简洁。
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> res(rowIndex+1);
for(int i=0;i<rowIndex+1;i++){
res[0]=1;
for(int j=i;j>=1;j--)
res[j]=res[j-1]+res[j];
}
return res;
}
};
我爱吃火锅