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. Inheritance and subclassing in Go - or its near likeness

    原文: http://golangtutorials.blogspot.com/2011/06/inheritance-and-subclassing-in-go-or.html ---------- ...

  2. webpack 配置react脚手架(五):mobx

    1.  配置项.使用mobx,因为语法时es6-next,所以先配置 .babelrc 文件 { "presets": [ ["es2015", { " ...

  3. 关于Mybatis查询结果的封装

    1.结果封装为List<Object> 接口示例: public List<Members> selectMembersListByName(String name); 配置文 ...

  4. Selenium常用API的使用java语言之13-多表单切换

    在 Web 应用中经常会遇到 frame/iframe 表单嵌套页面的应用, WebDriver 只能在一个页面上对元素识别与 定位, 对于 frame/iframe 表单内嵌页面上的元素无法直接定位 ...

  5. Solr添加paoding分词器

    1.Solr3.6.2 并可运行 paoding-analysis3.0.jar  下载 2.1 解压{$Solr-Path}/example/webapp 下的solr.war文件,解压到当前文件夹 ...

  6. 洛谷P1050 循环【java大数】

    题目:https://www.luogu.org/problemnew/show/P1050 题意:给定一个数$n$,问$n$的幂次的最低$k$位的循环节是多少. 思路:这真是我做过最难的java大数 ...

  7. 47、[源码]-Spring容器创建-初始化MessageSource

    47.[源码]-Spring容器创建-初始化MessageSource 7.initMessageSource();初始化MessageSource组件(做国际化功能:消息绑定,消息解析): 获取Be ...

  8. 没有向ZK写offset数据

    两天公司要学习kafka,结合之前的storm,做了一个简单的集成,之前也参考了网上的例子一些例子,发现或多或少都有一些问题.所以自己做了一个. 这个是网上其他人遇到的问题,给摘录一下,防止以后自己和 ...

  9. Linux gdb分析core dump文件

    文章目录1. coredump1.1 coredump简介1.2 coredump的文件存储路径1.3 coredump产生的条件1.4 coredump产生原因2. 测试生成coredump1. c ...

  10. nginx + keepalived双活配置

    一.安装nginx 我们这边默认安装的nginx的是1.12.2的版本,所以我们需要安装1.16.1版本的nginx的,才好,所以我们这边先更新yum源,步骤如下: 1.添加yum源: [root@s ...