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

Subscribe to see which companies asked this question

【思路】

这道题目很简单,代码如下:

 public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> Pascal = new ArrayList<>();
if(numRows == 0) return Pascal;
List<Integer> list = new ArrayList<>();
list.add(1);
Pascal.add(list);
if(numRows == 1) return Pascal; for(int i = 1; i < numRows; i++){
list = new ArrayList<>();
list.add(1);
List<Integer> prelist = Pascal.get(i-1);
int prelen = prelist.size();
for(int j = 0; j < prelen - 1; j++){
list.add(prelist.get(j) + prelist.get(j+1));
}
list.add(1);
Pascal.add(list);
}
return Pascal;
}
}

LeetCode OJ 118. Pascal's Triangle的更多相关文章

  1. 【一天一道LeetCode】#118. Pascal's Triangle

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

  2. 【LeetCode】118. Pascal's Triangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  3. LeetCode OJ 119. 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, ...

  4. 【LeetCode】118. Pascal's Triangle

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

  5. LeetCode OJ:Pascal's Triangle(帕斯卡三角)

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

  6. 【LeetCode OJ】Pascal's Triangle II

    Problem Link: http://oj.leetcode.com/problems/pascals-triangle-ii/ Let T[i][j] be the j-th element o ...

  7. 【LeetCode OJ】Pascal's Triangle

    Prolbem Link: http://oj.leetcode.com/problems/pascals-triangle/ Just a nest-for-loop... class Soluti ...

  8. 【一天一道LeetCode】#119. Pascal's Triangle II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  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. HDU 3362 Fix(状压dp)

    Fix Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  2. Xcode-之Code Snippets Library

    一.说明 Code Snippets Library 为代码片段库,在开发项目过程中经常会遇到一些重复的代码块,创建代码片段库可以减少我们开发的工作量,而且非常方便调用.Xcode系统中也为我们提供了 ...

  3. SimpleDateFormat类

    /* * DateFormat类概述 * DateFormat是日期时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间 * 是抽象类,所以使用其子类SimpleDateFormat * ...

  4. 在magento里把查询语句转换称sql语句

    在magento里把查询语句转换称sql语句 $order->getSelectsql(true); order为对象.

  5. 设计新Xlator扩展GlusterFS[转]

    原文:http://www.linuxidc.com/Linux/2013-08/89105.htm 1. GlusterFS概述 GlusterFS是一个开源的分布式文件系统,具有强大的Scale- ...

  6. JMS理解2

    使用JMS 的应用程序被称为JMS 客户端,处理消息路由与传递的消息系统被称为JMS Provider,而JMS 应用则是由多个JMS 客户端和一个JMS Provider 构成的业务系统.发送消息的 ...

  7. hdu_5831_Rikka with Parenthesis II(模拟)

    题目链接:hdu_5831_Rikka with Parenthesis II 题意: 给你一些括号的排列,必须交换一次,能不能将全部的括号匹配 题解: 模拟一下括号的匹配就行了,注意要特判只有一对括 ...

  8. OpenCV2.x自学笔记——固定阈值

    threshold( const CvArr* src,  CvArr* dst,  double threshold,  double max_value,  int threshold_type) ...

  9. art中的部分内容,留着慢慢研究

    root@hbg:/tmp# cat /proc/mtddev:    size   erasesize  namemtd0: 00040000 00010000 "u-boot" ...

  10. ESFramework 4.0 进阶(04)-- 驱动力:通信引擎(下)

    在ESFramework 4.0 进阶(03)-- 驱动力:通信引擎(上)一文中,我们对ESFramework提供的每一个通信引擎的接口都做了详细了说明,这篇文章我们将继续探讨这些接口的实现类 -- ...