问题描述:

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

问题分析:

第n行的数据是在第n-1行的基础上计算出来的。是一个迭代的过程

解决方法:

//生成杨辉三角的前n行
public static List<List<Integer>> generate(int numRows) {
List<List<Integer>> list = new ArrayList<List<Integer>>(); //定义list
if(numRows <= 0) return list;
List<Integer> row = new ArrayList<Integer>(); //第一行
row.add(1);
list.add(row);
for(int i = 2; i <= numRows; i++){ //生成后面的n-1行数据
List<Integer> l = new ArrayList<Integer>(); //存储第i行数据
l.add(1);
row = new ArrayList<Integer>(list.get(list.size() - 1)) ; //获得上一行数据
for(int j = 0; j < row.size() - 1; j++){
int e = row.get(j) + row.get(j + 1);
l.add(e);
} l.add(1); list.add(l);
}
return list;
}

118. Pascal's Triangle (java)的更多相关文章

  1. leetcode 118 Pascal's Triangle ----- java

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

  2. 118. Pascal's Triangle

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

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

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

  4. LN : leetcode 118 Pascal's Triangle

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

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

  6. Java for LeetCode 118 Pascal's Triangle

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

  7. Java [Leetcode 118]Pascal's Triangle

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

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

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

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

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

随机推荐

  1. 深入了解JVW

    Java内存组成介绍:堆(Heap)和非堆(Non-heap)内存 按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟机启动时 ...

  2. 古堡算式|2012年蓝桥杯B组题解析第二题-fishers

    (4')古堡算式 福尔摩斯到某古堡探险,看到门上写着一个奇怪的算式: ABCDE * ? = EDCBA 他对华生说:"ABCDE应该代表不同的数字,问号也代表某个数字!" 华生: ...

  3. 怎么在父窗口调用它页面的iframe里面数据,进行操作?

    注:在服务器下操作有效果,本地无效 document.getElementById("taskdetail1").contentWindow.test(a) document.ge ...

  4. MongoDB集群配置笔记一

    MongoDB 的部署方案有单机部署.复本集(主备)部署.分片部署.复本集与分片混合部署.混合的部署方式如图: 分片集群的构造 (1)mongos :数据路由,和客户端打交道的模块.mongos本身没 ...

  5. 几个C++ online test 网站

    http://www.mycppquiz.com/list.php http://www.codelect.net/TestDetails/Cplusplus-Senior-Level-Test ht ...

  6. --HTML标签1

    文字标签: <h>标签 标题,分为<h1>-<h6>(6级) <b>  加粗 <u> 下滑线 <s>或<strike> ...

  7. 网站项目所有js css无法引用问题解决方案

    网站页面中的所有js css引用失效,路径确保正确,但是浏览器就是报找不到引用.仔细查找发现问题所在: 报错信息很详细了,就是.NET Framework 版本不同导致.同时也提供了两个解决方案:将. ...

  8. JaveWeb 公司项目(2)----- 类模态窗口显示DIV并将DIV放置在屏幕正中间

    上一篇博客写的是通过隐藏显示进行div的替换,接下来需要在原有的div前添加一个div,进行表单的提交,需要将div放置在正中间,然后类似C#中的模态窗口,在进行完新弹出的div操作之后,才可以进行下 ...

  9. synchronized中判断条件用while而不是if

    假设一个生产者生产一个产品,两个消费者A,B去取这个商品. 使用if: A去取商品,发现空,于是等待... B去取商品,发现空,于是等待... 生产者生产商品,唤醒他们 B先争到锁,从wait()后执 ...

  10. SpringBoot之profile的使用

    Profile配置是针对不同的环境提供不同的配置支持,比如,在开发环境的配置和测试环境下的配置不同,那么就可以使用Profile配置来实现该要求. 在你的src/main/resources下建立相应 ...