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 getRow(int rowIndex) { vector 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 getRow(int rowIndex) { vector 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;}