5/30/2014

55. Pascal's Triangle II

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

1 条评论:

  1. [1]
    [1 1]
    [1 1 1]-->[1 2 1]
    [1 2 1 1]-->[1 3 3 1]
    [1 3 3 1 1]->[1 4 6 4 1]

    backwards.
    This method is really brilliant.

    回复删除