LeetCode: Pascal's Triangle

LeetCode: Pascal's Triangle_第1张图片

public class Solution {
    public List<List<Integer>> generate(int numRows) {
        ArrayList<Integer> levelList = new ArrayList<Integer>();
        ArrayList<List<Integer>> treeList = new ArrayList<List<Integer>>();
        if(numRows == 0){
            return treeList;
        }
        if(numRows == 1){
            levelList.add(1);
            treeList.add(levelList);
            return treeList;
        }

        levelList.add(1);
        treeList.add(levelList);
        ArrayList<Integer> pre;
        int size;
        for(int i=2; i<= numRows; i++){
            pre = levelList;
            levelList = new ArrayList<Integer>();
            size = pre.size();
            levelList.add(1);
            for(int j=1; j<size; j++){
                levelList.add(pre.get(j-1)+pre.get(j));
            }
            levelList.add(1);
            treeList.add(levelList);
        }
        return treeList;
    }
}

你可能感兴趣的:(LeetCode,pascal)