Pascal's Triangle II

ID: 119; easy

Solution 1

func getRow(rowIndex int) []int {
    res := make([]int, rowIndex + 1)
    res[0] = 1
    for i := 1; i < rowIndex+1; i++ {
        res[i] = res[i-1] * (rowIndex-i+1) / i
    }
    return res
}
(nm)=(nm1)×nm+1m{n\choose{m}}={n\choose{m-1}} \times \frac{n-m+1}{m}
class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<Integer> ans = new ArrayList<>();
        ans.add(1);
        long curElement = 1;
        for (int i = 1; i <= rowIndex; i++) {
            curElement = curElement * (rowIndex-i+1) / i;
            ans.add((int)curElement);
        }
        return ans;
    }
}

Last updated