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?
比较简单,就地更新,注意从后往前更新即可。
一直在想有没有什么公式直接可以求出第i行第k个数?有没有同学知道的,一定留言告诉我!谢啦!
#define vi vector<int>
class Solution {
public:
vi getRow(int row)
{
vi ans;
if (row<0)
return ans;
ans.push_back(1);
for(int i=1;i<=row;i++)
{
int k=ans.size();
for(int j=k-1;j>0;j--)
ans[j]+=ans[j-1];
ans.push_back(1);
}
return ans;
}
};