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?
trick在于从后往前扫,不会overwrite data
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> res(rowIndex+1);
res[0]=1;
// loop for each level
for (int i=0; i<=rowIndex; i++){
// scan from right, no overwriting
for (int j=i; j>0; j--)
res[j]+=res[j-1];
}
return res;
}
};