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 ...
随机推荐
- 使用userAgent判断使用的是什么浏览器
<script type="text/javascript"> function validB(){ var u_agent = Navigator.userAgent ...
- OAuth的机制原理讲解及开发流程(转)
1.OAuth的简述 OAuth(Open Authorization,开放授权)是为用户资源的授权定义了一个安全.开放及简单的标准,第三方无需知道用户的账号及密码,就可获取到用户的授权信息,并且这是 ...
- 使用itext直接替换PDF中的文本
直接说问题,itext没有直接提供替换PDF中文本的接口(查看资料得到的结论是PDF不支持这种操作),不过存在解决思路:在需要替换的文本上覆盖新的文本.按照这个思路我们需要解决以下几个问题: itex ...
- 【JavaScript】浅析JavaScript中arguments对象的使用
arguments对象不能显式创建,arguments对象只有函数开始时才可用.函数的 arguments 对象并不是一个数组,访问单个参数的方式与访问数组元素的方式相同.索引 n 实际上是 argu ...
- Docker LNMP环境搭建
原文地址:https://www.awaimai.com/2120.html 1 快速使用 2 安装docker和docker-compose 3 使用国内镜像仓库 4 目录说明 4.1 目录结构 4 ...
- Jquery 选择器 详解 js 判断字符串是否包含另外一个字符串
Jquery 选择器 详解 在线文档地址:http://tool.oschina.net/apidocs/apidoc?api=jquery 各种在线工具地址:http://www.ostools ...
- 【转】DSL
DSL DSL 时不时地会成为一个话题,所以今天想专门说一下. DSL 也就是 Domain Specific Language 的简称,是指为某些特定领域(domain)设计的专用语言.举个例子,L ...
- javascript控件(二):一个好用的表格(分页实例)
一.官网 https://datatables.net/ 二.引用 <script src="bower_components/datatables.net/js/jquery.dat ...
- Android SDK Manager详解
Android基础知识——Android SDK Manager详解 做Android开发时,免不了使用Android SDK Manager,安装需要的sdk版本.buildTools版本等等. ...
- maven依赖包冲突解决办法
今天在写一个demo时报了以下错误 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding /slf4j-lo ...