LeetCode119. Pascal's Triangle II
Description
Given an index k, return the kth row of the Pascal’s triangle.
For example, given k = 3,
Return [1,3,3,1].
my program
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> res = {1};
for(int i = 1; i<=rowIndex; i++)
{
res.push_back(0);
for(int j = res.size(); j>0; j--)
{
res[j] += res[j-1];
}
}
return res;
}
};
34 / 34 test cases passed.
Status: Accepted
Runtime: 0 ms
other methods
vector<int> getRow(int rowIndex) {
vector<int> ans(rowIndex+1,1);
int small = rowIndex/2;
long comb = 1;
int j = 1;
for (int i=rowIndex; i>=small; i--){
comb *= i;
comb /= j;
j ++;
ans[i-1] = (int)comb;
ans[j-1] = (int)comb;
}
return ans;
}