118. Pascal's Triangle

Easy

Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.


In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
package leetcode.easy;

import java.util.ArrayList;
import java.util.List; public class PascalSTriangle {
@org.junit.Test
public void test() {
System.out.println(generate(5));
} public List<List<Integer>> generate(int numRows) {
List<List<Integer>> triangle = new ArrayList<List<Integer>>(); // First base case; if user requests zero rows, they get zero rows.
if (numRows == 0) {
return triangle;
} // Second base case; first row is always [1].
triangle.add(new ArrayList<Integer>());
triangle.get(0).add(1); for (int rowNum = 1; rowNum < numRows; rowNum++) {
List<Integer> row = new ArrayList<>();
List<Integer> prevRow = triangle.get(rowNum - 1); // The first row element is always 1.
row.add(1); // Each triangle element (other than the first and last of each row)
// is equal to the sum of the elements above-and-to-the-left and
// above-and-to-the-right.
for (int j = 1; j < rowNum; j++) {
row.add(prevRow.get(j - 1) + prevRow.get(j));
} // The last row element is always 1.
row.add(1); triangle.add(row);
} return triangle;
}
}

LeetCode_118. Pascal's Triangle的更多相关文章

  1. [LeetCode] Pascal's Triangle II 杨辉三角之二

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  2. [LeetCode] Pascal's Triangle 杨辉三角

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  3. 【leetcode】Pascal's Triangle II

    题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...

  4. 【leetcode】Pascal's Triangle

    题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

  5. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  6. LeetCode 119 Pascal's Triangle II

    Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...

  7. LeetCode - Pascal's Triangle II

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...

  8. 【leetcode】Pascal's Triangle I & II (middle)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  9. 118. Pascal's Triangle

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...

随机推荐

  1. python练习题(一)

    背景: 和公司的二位同事一起学习python,本着共同学习.共同成长.资源共享的目标,然后从中学习,三人行必有我师 练习题更新中······ 题目: 输入一个值num,如果 num 大于 10,输出: ...

  2. WebStorm 安装

    官方下载地址:https://www.jetbrains.com/webstorm/ 下载 安装 等待.......... 安装完成........................  开始使用(第一次 ...

  3. scoket --- 练习

    三次握手,四次挥手(面试会问) 三次握手建连 [] 最开始的时候客户端和服务器都是处于CLOSED状态.主动打开连接的为客户端,被动打开连接的是服务器. TCP服务器进程先创建传输控制块TCB,时刻准 ...

  4. 关于 sublime 的插件 AdvancedNewFile 新建文件/文件夹 插件

    新建文件的插件: 快捷键:Ctrl + N 路径:当前目录下进行创建:js/index.js      表示在当前js目录下面创建index.js box      表示直接在当前目录下面创建一个bo ...

  5. h5css3弹性盒子

    弹性盒子: 老:display:box: 新:display:flex: 方向:flex-direction: 横向正方向 row/横向反方向 row-reverse/纵向正方向 column/纵向反 ...

  6. Linux disk 100% busy,谁造成的?

    disk 100% busy,谁造成的? 2016/11/16 vmunix iostat等命令看到的是系统级的统计,比如下例中我们看到/dev/sdb很忙,如果要追查是哪个进程导致的I/O繁忙,应该 ...

  7. Cashe的使用

    1.CacheHelper public class CacheHelper { public static ObjectCache Cache { get { return MemoryCache. ...

  8. linux产看磁盘信息命令-lsblk,blkid,dumpe2fs

    一.lsblk命令用于列出所有可用块设备的信息,而且还能显示他们之间的依赖关系,但是它不会列出RAM盘的信息.块设备有硬盘,闪存盘,cd-ROM等等.lsblk命令包含在util-linux-ng包中 ...

  9. wepy代码知识点

    index-page <style lang="less"> .index-nood-modal { width: 100vw; height: 100vh; posi ...

  10. sql 查出相同的记录 并把相同记录 显示在一起

    select c.workunit unitname,a.positionid,a.positiontype,a.isfirst,a.mastersort,a.directoraudit, c.wri ...