LeetCode 119. Pascal's Triangle II

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

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

The ith row, kth values actually is C(i, k) .... This is a math question.

    vector<int> getRow(int rowIndex) {
        vector<int> res;
        res.push_back(1);
        int k = rowIndex;
        for(int i = 1; i <= rowIndex; ++i, --k) {
            double tmp = ((double)res[i-1]*k)/i;
            res.push_back((int)tmp);
        }
        return res;
    }


你可能感兴趣的:(LeetCode 119. Pascal's Triangle II)