0
点赞
收藏
分享

微信扫一扫

119. Pascal's Triangle II


题目:



Given an index k, return the kth

For example, given k = 3,

Return ​​[1,3,3,1]​​.

Note:
Could you optimize your algorithm to use only O(k) extra space?

思路:

本题由​​上一篇博客​​衍生而来,方法同上一篇博客,只是不需要额外数组来存储所有vector数组

代码:

class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> res(0);
if(rowIndex<0)
return res;
res.push_back(1);
for(int i =1; i<=rowIndex;i++)
{
vector<int> temp;
temp.push_back(1);
for(int j=0;j<res.size()-1;j++)
{
temp.push_back(res[j]+res[j+1]);
}
temp.push_back(1);
res = temp;
}
return res;
}
};





举报

相关推荐

0 条评论