2022-07-19 16:26:56 +02:00
|
|
|
class Solution {
|
2024-01-03 12:06:42 +01:00
|
|
|
int getN(const vector<int> &previousRow, int n) {
|
2022-07-19 16:26:56 +02:00
|
|
|
if (n == 0 || n == previousRow.size()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return previousRow[n - 1] + previousRow[n];
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
public:
|
|
|
|
vector<vector<int>> generate(int numRows) {
|
2022-07-19 16:26:56 +02:00
|
|
|
if (numRows <= 0) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
vector<vector<int>> result{vector<int>{1}};
|
2022-07-19 16:26:56 +02:00
|
|
|
for (auto i = 2; i <= numRows; i++) {
|
2024-01-03 12:06:42 +01:00
|
|
|
auto &previous = result.back();
|
2022-07-19 16:26:56 +02:00
|
|
|
vector<int> current;
|
|
|
|
|
|
|
|
for (auto j = 0; j < i; j++) {
|
|
|
|
current.push_back(getN(previous, j));
|
|
|
|
}
|
|
|
|
result.push_back(move(current));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|