[LeetCode] Pascal's Triangle II

119. Pascal’s Triangle II

Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal’s triangle.

In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
vector<int> getRow(int rowIndex) {
if(!rowIndex) return {1};
vector<int> res = {1,1}, prev;
while(--rowIndex) {
swap(prev,res);
res.clear();
res.push_back(1);
for(int i = 0; i < prev.size() - 1; i++) {
res.push_back(prev[i] + prev[i+1]);
}
res.push_back(1);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/12/PS/LeetCode/pascals-triangle-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.