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
代码语言:javascript复制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;
}
};