题目描述:

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

解题思路:

每一行都是在前一行的基础上在第一个位置插上1,然后其余的部分两两相加得到新的值。

注意每次添加进母list中的子list必须新开辟空间,否则所有的子list全部指向相同的对象。

代码如下:

public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
List<Integer> rowList = new ArrayList<Integer>();
for(int i = 1; i <= numRows; i++){
rowList.add(0,1);
for(int j = 1; j < i - 1; j++){
rowList.set(j, rowList.get(j) + rowList.get(j + 1));
}
list.add(new ArrayList<Integer>(rowList));
}
return list;
}
}

  

Java [Leetcode 118]Pascal's Triangle的更多相关文章

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

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

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

  3. LeetCode 118 Pascal's Triangle

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

  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. Java for LeetCode 118 Pascal's Triangle

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

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

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

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

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

  8. Java [Leetcode 119]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 118 Pascal's Triangle 数论递推

    杨辉三角,即组合数 递推 class Solution { vector<vector<int>> v; public: Solution() { ; i < ; ++i ...

随机推荐

  1. java调用dll文件中的类型转换

    char *转String   (env)->NewStringUTF("the content you want to type in"); char *转jbyteArr ...

  2. [转载]Spring Beans Auto-Wiring

    Autowiring Modes You have learnt how to declare beans using the <bean> element and inject < ...

  3. POJ 3292

    Semi-prime H-numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7059   Accepted: 3 ...

  4. intellij idea 12、13 win8 下 中文输入覆盖的问题(搜狗输入法或者其他输入法)

    最近升级到idea12,发现中文输入存在问题,输入中文的时候会出现空格,并且覆盖后面的字符,这个问题让我很郁闷. 假设idea的安装位置为:D:\Program Files\JetBrains\Int ...

  5. lintcode:子数组之和为0

    题目: 子数组之和 给定一个整数数组,找到和为零的子数组.你的代码应该返回满足要求的子数组的起始位置和结束位置 样例 给出[-3, 1, 2, -3, 4],返回[0, 2] 或者 [1, 3]. 解 ...

  6. 欧拉工程第74题:Digit factorial chains

    题目链接:https://projecteuler.net/problem=74 数字145有一个著名的性质:其所有位上数字的阶乘和等于它本身. 1! + 4! + 5! = 1 + 24 + 120 ...

  7. windows下安装ubantu

        首先声明我是一个linux大菜鸟,之所以学这个,一个是好玩,另外做DL的一些软件如Caffe要在这个平台上运行,所以没事就鼓捣鼓捣.linux是一种内核,市场上支持这种内核的操作系统有uban ...

  8. [topcoder]BoxesDiv2

    https://community.topcoder.com/stat?c=problem_statement&pm=13192 #include <vector> #includ ...

  9. stringUtils是apache下的Java jar补充包

    org.apache.commons.lang.StringUtils StringUtils中一共有130多个方法,并且都是static的, 所以我们可以这样调用StringUtils.xxx().

  10. mysql23个知识点

    1.它是一种解释语言:写一句执行一句,不需要整体编译执行. 2.1.没有“ ”,字符串使用‘ '包含 3.一个表只有一个主键,但是一个主键可以是由多个字段组成的 组合键 4.实体完整性:实体就是指一条 ...