118. Pascal's Triangle

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

一刷

public class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> res = new ArrayList<>();
        if(numRows<=0) return res;
        List<Integer> list = new ArrayList<>();
        list.add(1);
        res.add(new ArrayList<>(list));
        for(int i=1; i<numRows; i++){
            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-1) + res.get(i-1).get(j));
            }
            res.add(new ArrayList<>(list));
        }
        return res;  
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,794评论 0 33
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,156评论 0 23
  • 孩子们,啤酒倒在杯子里,泡沫迅速满上杯口,蓬蓬的,松松的、洁白无暇,这是啤酒原来的样子?不对,真正的啤酒模样...
    久久0820阅读 332评论 0 0
  • 所有故事的开始仿佛都是为了结束,可是我依然想去爱,相信爱,我想,这应该就是我想要的人生吧! 09年的10月,是我和...
    OK迷你仓阅读 164评论 0 1