Pascal’s Triangle II

Desicription

Given an index k, return the kth row of the Pascal’s triangle.

For example, given k = 3,
Return [1,3,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
private:
int C(double A, double B) {
double res = 1;
for(int i = 1; i <= B; i++)
res *= (double)(A-i+1)/i;
return res+0.5;
}
public:
vector<int> getRow(int rowIndex) {
vector<int> res;
for(int i = 0; i <= rowIndex; i++)
res.push_back(C(rowIndex, i));
return res;
}
};