LeetCode——Pascal's Triangle II

Description:

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        for(int i=0; i<=rowIndex; i++) {
            List<Integer> tList = new ArrayList<Integer>();
            if(i == 0) {
                tList.add(1);
            }
            else {
                for(int j=0; j<=i; j++) {
                    if(j == 0 || j == i) {
                    tList.add(1);
                }
                else {
                    tList.add(list.get(i-1).get(j) + list.get(i-1).get(j-1));
                }
                }
            }
            list.add(tList);
        }
        return list.get(rowIndex);
         
    }
}

 

posted @   Pickle  阅读(199)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示