LeetCode 0119 - Pascal's Triangle II

2021-08-11 10:57:08 浏览数 (1)

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;
    }
};

0 人点赞