LeetCode118.杨辉三角
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。
示例:
输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
] 解析:帕斯卡三角,又称杨辉三角,给一个行数,输出杨辉三角,需要结合杨辉三角的性质。我们主要根据这条性质来产生结果:每个数字等于上一行的左右两个数字之和。可用此性质写出整个杨辉三角。即第n+1行的第i个数等于第n行的第i-1个数和第i个数之和。
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> list = new ArrayList<>();
for (int i=0;i<numRows;i++) {
list.add(0,1);
for (int j=1;j<list.size()-1;j++) {
list.set(j,list.get(j)+list.get(j+1));
}
res.add(new ArrayList<>(list));
}
return res;
}
}
LeetCode118.杨辉三角的更多相关文章
- [Swift]LeetCode118. 杨辉三角 | Pascal's Triangle
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- LeetCode118 杨辉三角 Python
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 示例:输入: 5 输出: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] cl ...
- [leetcode-118]Pascal's triangle 杨辉三角
Pascal's triangle (1过) Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- LeetCode118. Pascal's Triangle 杨辉三角
题目 给定行数,生成对应的杨辉三角 思考 同一行是对称的,最大的下标为(行数+1)/2;1,1,2,3,6;下标从0开始,则对应分别为0.0.1.1.2.2 对于第偶数行,个数也是偶数,对于奇数行,个 ...
- [LeetCode] 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, ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- POJ2167Irrelevant Elements[唯一分解定理 组合数 杨辉三角]
Irrelevant Elements Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2407 Accepted: 59 ...
- python生成器实现杨辉三角
def triangels(): """ 杨辉三角 """ lst = [1] n_count = 2 # 下一行列表长度 while Tr ...
- python 生成器生成杨辉三角
用Python写趣味程序感觉屌屌的,停不下来 #生成器生成展示杨辉三角 #原理是在一个2维数组里展示杨辉三角,空的地方用0,输出时,转化为' ' def yang(line): n,leng=0,2* ...
随机推荐
- A little issue in Mathematical Thought from Ancient to Modern Times, Vol. 3
At P985 of the book, says But there are cuts that are not determined by rational numbers. If we put ...
- ST表模板 Balanced Lineup POJ3264
http://poj.org/problem?id=3264 题意 rmq max min之差 模板: #define _CRT_SECURE_NO_WARNINGS #include<cmat ...
- Oracle的一些经典SQL面试题
实例1:测试数据: create table nba( team varchar2(20), year number(4) ) SQL> select * from nba; TEAM ...
- 【基本功】Java动态追踪技术探究 不重启JVM,替换掉已经加载的类?不重启JVM,获知运行时对象的属性
https://mp.weixin.qq.com/s/_hSaI5yMvPTWxvFgl-UItA 小结: 1.根据Java的类加载机制,在同一个ClassLoader中,类是不允许重复的: 2.单例 ...
- [elastic search][redis] 初试 ElasticSearch / redis
现有项目组,工作需要. http://www.cnblogs.com/xing901022/p/4704319.html Elastic Search权威指南(中文版) https://es.xiao ...
- delphi inttohex 整型到十六进制
inttohex from delphi help: Returns the hex representation of an integer. Unit SysUtils Category nume ...
- 树和二叉树->存储结构
文字描述 1 二叉树的顺序存储 用一组地址连续的存储单元自上而下,自左至右存储完全二叉树上的结点元素. 这种顺序存储只适用于完全二叉树.因为,在最坏情况下,一个深度为k且只有k个结点的单支树却需要长度 ...
- python之路(转载)
Python之路 Python生产环境部署(fastcgi,uwsgi) Django 缓存.队列(Memcached.redis.RabbitMQ) Python(九) Python ...
- python摸爬滚打之day010----函数进阶
1.函数动态传参 *args : 将所有的位置参数打包成一个元组的形式. **kwargs : 将所有的关键字参数打包成一个字典的形式. 形参的接收顺序: 位置参数 > *args > ...
- selenium+iframe 如何定位元素(实战)
场景: 在同一界面,需定位iframe里面的元素, 就需要切换至Iframe块,然后定位元素,验证完成后,再切换出来. 如果不切换至iframe ,会发现不管采取什么定位,都会报元素不存在.