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

题目标签:Array

  题目给了我们一个numRows,让我们写出这个行数的杨辉三角。来观察一下原题例子,5行的话,第一行只有1,第二行,只有1,第三行,除去第一个1和最后一个1,中间的都是上一行的两边数字之和。所以,我们只需要设定,每一行,第一个数字为1,最后一个为1,中间的数字,都由上一行同样位置的数字 + 前一个就可以了。

Java Solution:

Runtime beats 19.71%

完成日期:04/05/2017

关键词:Array

关键点:第一和最后都为1,中间由上一行同样位置数字 + 前一个数字

 public class Solution
{
public List<List<Integer>> generate(int numRows)
{ List<List<Integer>> pt = new ArrayList<>(); // create numRows List.
for(int i=0; i < numRows; i++)
{
List<Integer> list = new ArrayList<>();
// each row's first and last element is 1.
for(int j=0; j <= i; j++)
{
if(j == 0)
list.add(1);
else if(j == i)
list.add(1);
else // the middle element is sum of last row's same index-1 and index.
list.add( pt.get(i-1).get(j-1) + pt.get(i-1).get(j) ); } pt.add(list); // add this row into pt.
} return pt;
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

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(杨辉三角)

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

  7. LeetCode 118 Pascal's Triangle

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

  8. LN : leetcode 118 Pascal's Triangle

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

  9. 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. Could not chdir to home directory /home/USER: Permission denied

    Could not chdir to home directory /home/USER: Permission denied  2 years ago davidzhang We changed t ...

  2. MyBatis学习(六)MyBatis关联映射之一对多映射

    数据库中一对多通常使用主外键关联,外键应该在多方,即多方维护关系. 下面举一个简单实例来看看MyBatis怎么处理一对多的关系. 1.创建一个项目,导入所需jar包,导入db.properties配置 ...

  3. MySql join on 和 where

    原文:http://www.cnblogs.com/Jessy/p/3525419.html left join :左连接,返回左表中所有的记录以及右表中连接字段相等的记录. right join : ...

  4. 关于String的对象创建

    1)String String是Java中的字符串类,属于引用数据类型.所以String的对象存放的是引用的地址.在底层是一个字符型数组. String是不可变的.所谓的不可变是指一个对象有了一个引用 ...

  5. 【maven插件】maven-shade-plugin

    概述 该插件提供了将artifact打包到一个本地jar包的能力,包括其依赖关系以及一些参数如 shade -rename重命名依赖关系的包. 目标 shade:shade 绑定到建生命周期中的pac ...

  6. 怎样使用自定义标签简化 js、css 引入?

    国庆将至,工作兴致全无,来总结点项目里平时不起眼干货. 前端引入 js .css 一般是这样: <script type="text/javascript" src=&quo ...

  7. Suneast & Daxia (规律)

    Suneast & Daxia Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u ...

  8. 渣渣学QT

    初学QT,自己的一些小总结,错误之处,望大神指点一二: 1,添加资源文件时想应用在界面的背景,但是发现用不了,后来才知道是没有"构建"?应该是要在构建之后才将所添加的资源文件真正的 ...

  9. 网时|云计算的集群技术对于传统IDC而言,又有哪些提高呢?

    当传统的IDC产品已经不足以满足现在科技的飞速发展时,云计算便应运而生.咱们暂且不论云计算在其他领域的贡献,仅IDC来讲,云计算的集群技术对于传统IDC而言,又有哪些提高呢? 1.服务类型 常用的传统 ...

  10. python爬取煎蛋网图片

    ``` py2版本: #-*- coding:utf-8 -*-#from __future__ import unicode_literimport urllib,urllib2,timeimpor ...