118. 杨辉三角Leetcode

/*
思路:
第一行有一个数,第二行有两个数,第n行有n个数

*/

class Solution {
    public List<List<Integer>> generate(int numRows) {

        List<List<Integer>> res = new ArrayList<>();

        if(numRows <= 0) return res;

        for(int i = 0; i < numRows; i++){
            List<Integer> list = new ArrayList<>();
            for(int j = 0; j <= i; j++){
                if(j == 0 || j == i){
                    list.add(1);
                }else{
                    list.add(res.get(i-1).get(j) + res.get(i-1).get(j-1));
                }
            }
            res.add(list);
        }

        return res;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_32682177/article/details/81949415