给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex 行。
在「杨辉三角」中,每个数是它左上方和右上方的数的和。

示例 1:
输入: rowIndex = 3 输出: [1,3,3,1]
示例 2:
输入: rowIndex = 0 输出: [1]
示例 3:
输入: rowIndex = 1 输出: [1,1]
提示:
0 <= rowIndex <= 33
进阶:
你可以优化你的算法到 O(rowIndex) 空间复杂度吗?
代码:
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<vector<int>> vec(rowIndex + 1);
for (auto i = 0; i <= rowIndex; i++) {
vec[i].resize(i + 1);
vec[i][0] = vec[i][i] = 1;
for (auto j = 1; j < i; j++) {
vec[i][j] = vec[i - 1][j] + vec[i - 1][j - 1];
}
}
return vec[rowIndex];
}
};
int main() {
int n; cin >> n;
Solution solution = Solution();
vector<int> res = solution.getRow(n);
for (auto i = 0; i < res.size(); i++) {
cout << res[i] << " ";
}
cout << endl;
return 0;
}
358

被折叠的 条评论
为什么被折叠?



