【leetcode】杨辉三角

版权声明:转载请注明 https://blog.csdn.net/qq_33591903/article/details/83685972

                                                  杨辉三角

一、要求

给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。

示例:

输入: 5
输出:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

二、思路

观察杨辉三角,不难得出,第i行的第j个数等于第i-1行第j个数加上第i-1行第j-1个数。


三、代码实现

public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        for (int i = 0; i < numRows; i++) {
            List<Integer> innerList = new ArrayList<>();
            for (int j = 0; j <= i; j++) {
                //当为某行第一个数或是最后一个数时,都为1
                if (j == 0 || j == i) {
                    innerList.add(1);
                    //否则第i行的第j个数等于第i-1行第j个数加上第i-1行第j-1个数
                } else {
                    innerList.add(list.get(i - 1).get(j) + list.get(i - 1).get(j - 1));
                }
            }
            //别忘了在外层list中添加内层list
            list.add(innerList);
        }
        return list;
    }

猜你喜欢

转载自blog.csdn.net/qq_33591903/article/details/83685972