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?
//#119 Pascal's Triangle II
//0ms 100%
class Solution {
public:
vector<int> getRow(int rowIndex)
{
vector<int> v0(1, 1);
//if(rowIndex == 0) return v0;
//v0.push_back(1);
for(int i=1; i<rowIndex+1; i++)
{
vector<int> v(v0.size()+1, 1);
for(unsigned int j=1; j<v.size()-1; j++)
{
v[j] = v0[j-1] + v0[j];
}
v0 = v;
}
return v0;
}
};