首页 > 其他 > 详细

118. 杨辉三角

时间:2020-04-12 19:47:01      阅读:55      评论:0      收藏:0      [点我收藏+]

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

技术分享图片

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

示例:

输入: 5
输出:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]
class Solution {
    public List<List<Integer>> generate(int row) {
        List<List<Integer>> lists = new LinkedList<>();
        if(row == 0) return lists;
        List<Integer> list = new LinkedList<>();
        list.add(1);
        lists.add(list);
        for(int i = 0;i < row - 1;i++){
            List<Integer> temList = lists.get(i);
            List<Integer> tem = new ArrayList<>();
            tem.add(1);
            for(int j = 0;j < temList.size() - 1;j++){
                tem.add(temList.get(j) + temList.get(j + 1));
            }
            tem.add(1);
            lists.add(new ArrayList<>(tem));
        }
        return lists;
    }
}

 

118. 杨辉三角

原文:https://www.cnblogs.com/zzytxl/p/12687128.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!