- Pascal’s Triangle II Add to List QuestionEditorial Solution My
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?
vector<int> result;
for( int i = 0; i<= rowIndex; i++)
{
if(i == 0)
{
result.push_back(1);
continue;
}
for(int j=i; j>=0; j--) //从后到前进行遍历
{
if(j==0)
{
result[0]=1;
}
else if(j==i)
{
result.push_back(1);
}
else
{
result[j] = result[j-1] + result[j];
}
}
}
return result;