Coding-Interview-101

Solutions to LeetCode problems filtered with companies, topics and difficulty.

View project on GitHub

Pascal’s Triangle II


Solution


    class Solution {
    public:
        vector<int> getRow(int rowIndex) {
            vector<int> ans(rowIndex + 1);
            for(int i = 0; i <= rowIndex; i++) {
                if(i == 0 || i == rowIndex)
                    ans[i] = 1;
                else
                    ans[i] = (int)(((long)ans[i - 1] * (long)(rowIndex + 1 - i)) / i);
            }
            return ans;
        }
    };