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* ...
随机推荐
- 关于ionic2 更新到ionic3 后组件不能用的解决方案
错误代码就不贴出来了,直接上代码吧! 首先在xx.module.ts添加 1.import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; 2.添加 ...
- wpf(第一章 基础知识)
wpf第一章基础知识:通过vs2015创建wpf程序会在引用里面多出3个核心程序集PresentationCore.PresentationFramework.WindowsBase.并且会在解决方案 ...
- [nginx] load balancing & location
一 将NGINX配置成7层load balancer,该怎么做? 参见: http://nginx.org/en/docs/http/load_balancing.html https://docs. ...
- [development][C] linux 设置线程名称
两个API, 都是linux的. 不是POSIX, 是GNU? 傻傻搞不清楚. 1. pthread_setname_np / pthread_setname_np 2. ptctl 带 PR_GE ...
- python发送邮件 大全汇总
https://blog.csdn.net/bmxwm/article/details/79007871 参考菜鸟教程发送只有文字的邮件 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...
- Windows7安装两个jdk配置
win7 配置两个jdk,之前装了jdk1.6,项目需要安装1.8. 首先去oracle官网下载一个和你eclipse版本一致的jdk(我的是32位). 网址:https://www.oracle.c ...
- 20165336 预备作业3 Linux安装及学习
Linux 安装及学习 一.VirtualBox和Ubuntu的安装 依照老师所给的步骤下载了VirtualBox 5.2.6和Ubuntu 16.04.3. 按照步骤一步一步进行了安装,出现的问题有 ...
- 【PyQt5-Qt Designer】pyqtSignal()-高级自定义信号与槽
PyQt 5信号与槽的几种高级玩法 参考:http://www.broadview.com.cn/article/824 from PyQt5.QtCore import QObject , pyqt ...
- vim 私人快捷键备忘录
i 上 k 下 j 左 l 右 ( 上移一段 ) 下移一段 * 搜索关键字 d 删除 y 复制 p 粘贴 h 插入 H 头插 o 下开一行 O 上开一行 f 后跳指定关键字 F 前跳指定关键字 e 字 ...
- sqlite数据导入mysql
sqlite导出数据 1.首先将sqlite数据库中的数据库格式由db或者db3等转为.sql格式,方法如下: 首先是不修改路径的情况下,在命令行下(方法一): sqlite3 database_na ...