Question

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

Key to the solution is to use two arrays, one for current list, one for previous array.

 public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (numRows == 0)
return result;
List<Integer> prev = new ArrayList<Integer>();
prev.add(1);
result.add(prev);
while (numRows > 1) {
List<Integer> current = new ArrayList<Integer>();
int length = prev.size();
current.add(1);
for (int i = 0; i < length - 1; i++)
current.add(prev.get(i) + prev.get(i + 1));
current.add(1);
result.add(current);
prev = current;
numRows--;
}
return result;
}
}

Pascal's Triangle 解答的更多相关文章

  1. Pascal's Triangle II 解答

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

  2. [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, ...

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

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

  4. 【leetcode】Pascal's Triangle II

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

  5. 【leetcode】Pascal's Triangle

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

  6. LeetCode 118 Pascal's Triangle

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

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

  8. LeetCode - Pascal's Triangle II

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

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

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

随机推荐

  1. Php开发完全跨站点跨域名单点(SSO)同步登录和注销

    From:http://www.cnblogs.com/JinkoWu/p/5056646.html 先来说说什么是单点登录(SSO).来自百科的介绍:SSO英文全称Single Sign On,单点 ...

  2. [置顶] Objective-C ,ios,iphone开发基础:自定义控件:Eg: UIButton

    第一步:新建一个工程,在 .h文件中坐如下声明: #import <UIKit/UIKit.h> @interface MyButtonViewController : UIViewCon ...

  3. 【转】手机web——自适应网页设计(html/css控制)

    手机web——自适应网页设计(html/css控制) 就目前形势来看,Web App 正是眼下的一个趋势和潮流,但是,对于Web App的设计可能大家有的不是很了解,下面就将整理好的网页设计的技巧奉献 ...

  4. hdu 5430 Reflect (数学推导题)

    Problem Description We send a light from one point on a mirror material circle,it reflects N times a ...

  5. iOS开发-Runtime详解(简书)

    简介 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的.比如: [receiver message]; // ...

  6. mycat实例(3)

    配置MyCat 4. 配置schema.xml schema.xml里面管理着MyCat的逻辑库.表,每张表使用的分片规则.分布在哪个DataNode以及DataSource上. 之前的例子: < ...

  7. Spark Streaming--实战篇

    摘要:      Sprak Streaming属于Saprk API的扩展,支持实时数据流(live data streams)的可扩展,高吞吐(hight-throughput) 容错(fault ...

  8. [Regular Expressions] Find a String that Precedes Another String ?= , ?!

    Let's image tow cases for the following string: var str = `foo foobar foobaz fooboo` First of all: w ...

  9. Android窗口管理服务WindowManagerService对窗口的组织方式分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8498908 我们知道,在Android系统中, ...

  10. 内外连接、组函数、DDL、DML和TCL

    前言 cross join ,是笛卡尔积:nature join 是自然连接. 正文 内外连接 inner join inner join 的inner能够省略. 内连接 在一个表中可以找到在还有一个 ...