Pascal's Triangle:

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]
]
已知行数生成帕斯卡三角。实际上只要有第i层,那么就能生成第i+1层。每次新生成的层加入最终集合中即可。
     public List<List<Integer>> generate(int numRows) {
List<List<Integer>> re = new ArrayList<List<Integer>>(); for(int i=0;i<numRows;i++) {
List<Integer> temp = new ArrayList<Integer>();
for(int j=0;j<=i;j++) {
if(j==0 || j==i)
temp.add(1);
else
temp.add(re.get(i-1).get(j-1)+re.get(i-1).get(j));
}
re.add(temp);
}
return re;
}
 

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,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

与第一题几乎一样,只不过不需要返回整个三角而只需要返回最后一层。全程只需要维护生成层和它上一层两个ArrayList即可。

     public List<Integer> getRow(int rowIndex) {
List<Integer> re = new ArrayList<Integer>();
for(int i=0;i<=rowIndex;i++) {
List<Integer> temp = new ArrayList<Integer>();
for(int j=0;j<=i;j++) {
if(j==0 || j==i)
temp.add(1);
else
temp.add(re.get(j-1) + re.get(j));
}
re = temp;
}
return re;
}

[Leetcode][JAVA] Pascal's Triangle I, II的更多相关文章

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

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

  2. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  3. [LeetCode] 119. Pascal's Triangle II 杨辉三角 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

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

  5. Java for 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 [1,3 ...

  6. leetcode 119 Pascal's Triangle II ----- java

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

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

  8. LeetCode:Pascal's Triangle I II

    LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...

  9. 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 [1,3, ...

随机推荐

  1. Matrix的一些知识

    1.什么是ColorMatrix ColorMatrix是一个颜色矩阵,它定义了一个 4*5 的float[]类型的矩阵 颜色矩阵,而图像的 RGBA 值则存储在一个5*1的颜色分量矩阵C中 所以为了 ...

  2. php产生随机数函数

    <?php function generate_code($length = 4) { return rand(pow(10,($length-1)), pow(10,$length)-1); ...

  3. Ms sql pivot unpivot

    --建表 create table dbo.orders ( orderid int not null primary key nonclustered, orderdate datetime not ...

  4. C++学习基础四——顺序容器和关联容器

    —顺序容器:vector,list,queue1.顺序容器的常见用法: #include <vector> #include <list> #include <queue ...

  5. Windows上x86程序正常但x64程序崩溃问题

    先看下面代码: #include <stdio.h> #include <windows.h> #include <memory> class Test { pub ...

  6. java 中小数点的处理

    第一种 BigDecimal bg = new BigDecimal(f); double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleVa ...

  7. (转)SpyGlass工具介绍

    Spyglass工具有五大模块: lint,  CDC(多时钟域检查), LP(低功耗),Constraint(约束),DFT(可测试性). 一,在RTL层面上预估芯片性能,从而引导设计人员开发出更加 ...

  8. c++学习笔记——字面值常量类

    字面值常量类:数据成员都是字面值类型的聚合类是字面值常量类.如果一个类不是聚合类,但是它符合一下要求,则它也是个字面值常量类: 1.数据成员都必须是字面值类型. 2.类必须至少含有一个constexp ...

  9. Xcode 字体 设置-- Xcode family没有显示的字体

    前往文件夹 -> /Users/user/Library/Developer/Xcode/UserData/FontAndColorThemes/  (user改为自己的用户名) -----如果 ...

  10. spi 子系统

    http://blog.csdn.net/ropenyuan/article/details/42269641 http://blog.chinaunix.net/uid-27406766-id-33 ...