Sunday, November 22, 2015

[leetcode]Pascal's Triangle II

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<Integer> result = new ArrayList<Integer>();
        if (rowIndex < 0) return result;
        int rolling[] = new int[rowIndex+1];
        rolling[0] = 1;
        for (int i = 1; i < rowIndex+1; i++){
            for (int j = i; j >= 1; j--){
                rolling[j] = rolling[j]+rolling[j-1];
            }
        }

        for (int i = 0; i < rowIndex+1; i++){
            result.add(rolling[i]);
        }
        return result;
    }
}

No comments:

Post a Comment