LeetCode: Pascal's Triangle 解题报告
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 解题报告的更多相关文章
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【LeetCode】118. Pascal's Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- LeetCode:Pascal's Triangle I II
LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
随机推荐
- MATLAB 的 cell 大法(单元格数组)
MATLAB 的 cell,称单元格数组 or 元胞数组:使用频率特别高,甚至比 struct 结构体还高. MATLAB文档给出的 cell 官方定义: A cell array is a coll ...
- Java正则应用
private List<String> find(String reg, String str) { Matcher matcher = Pattern.compile(reg).mat ...
- 颜色传感器TCS230及颜色识别电路(转)
摘要 TCS230是美国TAOS公司生产的一种可编程彩色光到频率的传感器.该传感器具有分辨率高.可编程的颜色选择与输出定标.单电源供电等特点:输出为数字量,可直接与微处理器连接.文中主要介绍TCS23 ...
- Rust 之 cargo(项目构建和包管理工具)
如果食用cargo来进行项目构建: 1. 执行 cargo new hello_cargo --bin ,执行完上面的操作之后,我们切换到hell_cargo目录下,可以看到一个文件(Cargo.to ...
- MySQL -- 全文检索(自然语言全文检索)
自然语言全文本检索缺省或者modifier被设置为in natural language mode,都是进行自然语言检索.对于表中的每一行,match()都会返回一个关联值. mysql> CR ...
- MySQL和ORACLE、SQL Server、PostgreSQL相比
- 再说Android RecyclerView局部刷新那个坑
RecyclerView局部刷新大家都遇到过,有时候还说会遇见图片闪烁的问题. 优化之前的效果: 优化之后的效果: 如果想单独更新一个item,我们通常会这样做,代码如下: mLRecyclerV ...
- J2EEweb开发中的缓存问题的研究
一般情况下,浏览器都会缓存已经访问过的页面内容,关于如何禁止浏览器缓存的介绍,在网上到处都有相关的文章,但是,关于浏览器如何利用缓存,如何处理缓存的讲解,却鲜有人谈及 .浏览器在访问已缓存过的资源时, ...
- 构建高性能J2EE应用的五种核心策略
对于J2EE,我们知道当开发应用时,在架构设计阶段的决定将对应用的性能和可扩展性产生深远的影响.现在当开发一个应用项目时,我们越来越多地注意到了性能和可扩展性的问题.应用性能的问题比应用功能的不丰富问 ...
- 记一次mysql的存储过程改写
最近在对公司以前的老项目做整理,发现以前同事在程序中许多模块都是多次调用几个分散的存储过程..这样做无疑消耗了连接池的连接数,甚至会导致连接不够的时候创建连接池导致数据库处理的消耗..以及到处调用连接 ...