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?
对于产生一个新的行用从后往前的方法来更新,这样就只需一个O(k)的空间。
1
1 1
1 2 1
1 3 3 1
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int>a(rowIndex+1);
a[0]=1;
for(int i=1;i<=rowIndex;i++){
for(int j=i;j>=0;j--){
if(j==i)
a[j]=1;
else if(j==0)
a[j]=1;
else
a[j]=a[j]+a[j-1];
}
}
return a;
}
};