Pascal's Triangle II

题目
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.

Note that the row index starts from 0.

答案

class Solution {
    public List getRow(int rowIndex) {
        List row = new ArrayList<>();
        row.add(1);
        if(rowIndex == 0) return row;

        for(int i = 1; i <= rowIndex; i++) {
            int left = 0;
            int row_size = row.size();
            for(int j = 0; j <= row_size; j++) {
                int curr = (j < row.size()) ? row.get(j) : 0;
                if(j < row.size())
                    row.set(j, left + curr);
                else
                    row.add(left + curr);
                left = curr;
            }
        }
        return row;
    }
}

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