Pascal's Triangle

Problem

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

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

Pascal's Triangle List: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]

Solution

public class Solution {
    public List<Integer> getRow(int k) {
        List<Integer> row = new ArrayList<>();
        for (int i = 0; i <= k; i++) {
            row.add(0, 1);
            for (int j = 1; j < row.size() - 1; j++) {
                row.set(j, row.get(j) + row.get(j + 1));
            }
        }
        return row; 
    }
}

Analysis

This is a sub-problem of Pascal's Triangle I

results matching ""

    No results matching ""