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]
]

solution:

ackage cn.magicdu;

import java.util.ArrayList;
import java.util.List; public class _118_Pascal_Triangle { public List<List<Integer>> generate(int numRows) {
if (numRows < 0) {
return null;
}
List<List<Integer>> l1 = new ArrayList<>();
if (numRows >= 1) {
List<Integer> list = new ArrayList<>();
list.add(1);
l1.add(list);
}
if (numRows >= 2) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(1);
l1.add(list);
}
if (numRows >= 3) {
for (int i = 3; i <= numRows; i++) {
List<Integer> list = new ArrayList<>();
List<Integer> prev = l1.get(i - 2);
list.add(1);
for (int j = 2; j <= i - 1; j++) {
list.add(prev.get(j - 2) + prev.get(j - 1));
}
list.add(1);
l1.add(list);
} } return l1;
} }

每天一道LeetCode--118. Pascal's Triangle(杨辉三角)的更多相关文章

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

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

  2. [leetcode-118]Pascal's triangle 杨辉三角

    Pascal's triangle (1过) Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  3. LeetCode118. Pascal's Triangle 杨辉三角

    题目 给定行数,生成对应的杨辉三角 思考 同一行是对称的,最大的下标为(行数+1)/2;1,1,2,3,6;下标从0开始,则对应分别为0.0.1.1.2.2 对于第偶数行,个数也是偶数,对于奇数行,个 ...

  4. 【LeetCode每天一题】Pascal's Triangle(杨辉三角)

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

  5. Pascal's Triangle(杨辉三角)

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

  6. LeetCode 118. Pascal's Triangle (杨辉三角)

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

  7. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  8. LeetCode 118 Pascal's Triangle

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

  9. LN : leetcode 118 Pascal's Triangle

    lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...

  10. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

随机推荐

  1. 开源原生JavaScript插件-CJPCD(省市区联动)

    一.前言 上两篇博客笔者对 JavaScript Module 模式,闭包等知识点做了简单介绍之后,我们今天开始正式开发一款属于自己的 JavaScript 插件.由于最近项目刚好用到地区选择这一块的 ...

  2. ajax 源生,jquery封装 例子 相同哈哈

    http://hi.baidu.com/7636553/item/bbcf5fc93c8c950aac092f22 ajax使用回调函数的例子(原生代码和jquery代码) 一. ajax代码存在的问 ...

  3. SQL Prompt 5.3.4.1

    Red Gate系列之三 SQL Server 开发利器 SQL Prompt 5.3.4.1 Edition T-SQL智能感知分析器 完全破解+使用教程   Red Gate系列之三 SQL Se ...

  4. "file:///" file 协议

    [问题] 在WLW中拖入本地图片文件,然后调试过程中,选中对应图片,看到获得的对应的html源码中,图片地址是这样的: href="file:///C:/Users/CLi/AppData/ ...

  5. C# 解压zip压缩文件

    此方法需要在程序内引用ICSharpCode.SharpZipLib.dll 类库 /// <summary> /// 功能:解压zip格式的文件. /// </summary> ...

  6. 部署WEB应用程序

    部署WEB应用程序: 1.在模板机上新建IIS站点 2.安装WebDeploy后在IIS控制台中导出站点为应用程序包 其站点在新虚机上必须存在,否则会报错,如下: 应用程序(C:\ProgramDat ...

  7. ssh连接阿里云一段时间不操作自动断开

    打开/etc/ssh/sshd_config 添加或修改: ClientAliveInterval 120 ClientAliveCountMax 0

  8. The internals of Python string interning

    JUNE 28TH, 2014Tweet This article describes how Python string interning works in CPython 2.7.7. A fe ...

  9. 信号之sleep函数

    #include <unistd.h> unsigned int sleep(unsigned int seconds); 返回值:0或未休眠够的秒数 此函数使调用进程被挂起,直到满足以下 ...

  10. HOWTO install Oracle 11g on Ubuntu Linux 12.04 (Precise Pangolin) 64bits

    安装了Ubuntu 12.04 64bit, 想在上面安装Oracle 11gr2,网上找了好多文档都没成功,最后完全参考了MordicusEtCubitus的文章. 成功安装的关键点:install ...