题目:
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;
}
};