Leetcode 119. Pascal's Triangle II (Easy) (cpp)
Tag: Array
Difficulty: Easy
/*
119. Pascal's Triangle II (Easy)
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1].
*/
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> res(1, 1);
if (rowIndex == 0) {
return res;
}
for (int i = 1; i <= rowIndex; i++) {
vector<int> tmp = res;
res.clear();
res.push_back(1);
for (int i = 0; i < tmp.size() - 1; i++) {
res.push_back(tmp[i] + tmp[i + 1]);
}
res.push_back(1);
}
return res;
}
};