1、题目描述
给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。
进阶:
你可以优化你的算法到 O(k) 空间复杂度吗?
2、示例
输入: 3 输出: [1,3,3,1]
3、题解
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> res(rowIndex + 1, 1);
int cur, pre;
for (int i = 0; i <= rowIndex; i++)
{
pre = 1;
for (int j = 1; j < i; j++)
{
cur = res[j];
res[j] += pre;
pre = cur;
}
}
return res;
}
};
int main()
{
Solution solute;
int rowIndex = 5;
vector<int> res = solute.getRow(rowIndex);
for_each(res.begin(), res.end(), [](const int& v) {cout << v << endl; });
return 0;
}