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]
] SOLUTION 1:
很easy的题。注意记得把List加到ret中。
比较简单,每一行的每一个元素有这个规律:
1. 左右2边的是1.
i, j 表示行,列坐标。
2. 中间的是f[i][j] = f[i - 1][j] + f[i - 1][j - 1]
不断复用上一行的值即可。
 public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> ret = new ArrayList<List<Integer>>(); for (int i = 0; i < numRows; i++) {
List<Integer> list = new ArrayList<Integer>();
for (int j = 0; j <= i; j++) {
if (j == 0 || i == j) {
list.add(1);
} else {
int sum = ret.get(i - 1).get(j - 1) + ret.get(i - 1).get(j);
list.add(sum);
}
} // BUG 1: forget this statement.
ret.add(list);
} return ret;
}
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/list/Generate.java

LeetCode: Pascal's Triangle 解题报告的更多相关文章

  1. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

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

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

  3. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  4. LeetCode:Pascal's Triangle I II

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

  5. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  6. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  7. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  8. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  9. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

随机推荐

  1. webDriver.Close() 和 webDriver.Quit() 、webDriver.Dispose() 的区别

    1. webDriver.Close() - Close the browser window that the driver has focus of //关闭当前焦点所在的窗口2. webDriv ...

  2. JAVA操作mysql(如何更加面向对象的操作数据库)

    既然谈到面向对象,所以,先把连接信息给搞个对象出来: public class DBInfo { private String driver; private String host; private ...

  3. ML之监督学习算法之分类算法一 ———— k-近邻算法(最邻近算法)

    一.概述 最近邻规则分类(K-Nearest Neighbor)KNN算法 由Cover 和Hart在1968年提出了最初的邻近算法, 这是一个分类(classification)算法 输入基于实例的 ...

  4. Android APP通用型拒绝服务、漏洞分析报告

    点评:记得曾经有段时间很多SRC平台被刷了大量APP本地拒绝服务漏洞(目前腾讯金刚审计系统已经可检测此类漏洞),移动安全团队发现了一个安卓客户端的通用型拒绝服务漏洞,来看看他们的详细分析吧. 0xr0 ...

  5. C#设计模式(6)——原型模式(Prototype Pattern) C# 深浅复制 MemberwiseClone

    C#设计模式(6)——原型模式(Prototype Pattern)   一.引言 在软件系统中,当创建一个类的实例的过程很昂贵或很复杂,并且我们需要创建多个这样类的实例时,如果我们用new操作符去创 ...

  6. React Native工程修改Android包名

    默认初始化的React Native工程,生成Android工程的时候,包名默认是React Native工程的名字,跟一般Android工程com.company.xxx不一样. 这时候就需要手动修 ...

  7. 【转载,整理】Linux性能监控

    一. 比较全的linux性能检测网站 1. 很好的网站,原文:http://os.51cto.com/art/201402/430050.htm 监测 cpu.内存.网络.IO等命令及工具   2.  ...

  8. Android颜色值(RGB)所支持的四种常见形式

    Android中颜色值是通过红(Red).绿(Green).蓝(Blue)三原色,以及一个透明度(Alpha)值来表示的,颜色值总是以井号(#)开头,接下来就是Alpha-Red-Green-Blue ...

  9. mark CodeGenerator

    基础权限开发框架 BMS = Spring boot + Mybatis plus + Shiro     BMS / bms-admin  / src  / main  / java  / com  ...

  10. linux进程后台运行,且关终端后继续运行

    ctrl+z,fg,bg什么的都无法实现这一点.因为关终端之后就可能出问题 常用的命令如下 nohup /home/user/yourcommand.sh & nohup /home/user ...