118. Pascal's Triangle (java)
问题描述:
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]
]
问题分析:
第n行的数据是在第n-1行的基础上计算出来的。是一个迭代的过程
解决方法:
//生成杨辉三角的前n行
public static List<List<Integer>> generate(int numRows) {
List<List<Integer>> list = new ArrayList<List<Integer>>(); //定义list
if(numRows <= 0) return list;
List<Integer> row = new ArrayList<Integer>(); //第一行
row.add(1);
list.add(row);
for(int i = 2; i <= numRows; i++){ //生成后面的n-1行数据
List<Integer> l = new ArrayList<Integer>(); //存储第i行数据
l.add(1);
row = new ArrayList<Integer>(list.get(list.size() - 1)) ; //获得上一行数据
for(int j = 0; j < row.size() - 1; j++){
int e = row.get(j) + row.get(j + 1);
l.add(e);
} l.add(1); list.add(l);
}
return list;
}
118. Pascal's Triangle (java)的更多相关文章
- leetcode 118 Pascal's Triangle ----- java
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 118. Pascal's Triangle
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- LN : leetcode 118 Pascal's Triangle
lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...
- leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle
118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...
- Java for LeetCode 118 Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- Java [Leetcode 118]Pascal's Triangle
题目描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【LeetCode】118. Pascal's Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
随机推荐
- 2199: [Usaco2011 Jan]奶牛议会 2-sat
链接 https://www.luogu.org/problemnew/show/P3007 https://www.lydsy.com/JudgeOnline/problem.php?id=2199 ...
- python 之 知识点(1)
在python是使用bif=built in functions 即内置函数 dir(__builtins__) 可以查看所有的内置函数.注:pycharm中无法使用,不知道原因 help(input ...
- oracle 之 安装后pl/sql登录报ora-12154
这个问题一开始困扰了很久. 查的资料是复制一小段代码到tnsnames.ora中 SID名 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = ...
- BZOJ5018: [Snoi2017]英雄联盟
Description 正在上大学的小皮球热爱英雄联盟这款游戏,而且打的很菜,被网友们戏称为「小学生」.现在,小皮球终于受不 了网友们的嘲讽,决定变强了,他变强的方法就是:买皮肤!小皮球只会玩N个英雄 ...
- P2257 YY的GCD(莫比乌斯反演)
第一次做莫比乌斯反演,推式子真是快乐的很啊(棒读) 前置 若函数\(F(n)\)和\(f(d)\)存在以下关系 \[ F(n)=\sum_{n|d}f(d) \] 则可以推出 \[ f(n)=\sum ...
- 论文笔记:Diffusion-Convolutional Neural Networks (传播-卷积神经网络)
Diffusion-Convolutional Neural Networks (传播-卷积神经网络)2018-04-09 21:59:02 1. Abstract: 我们提出传播-卷积神经网络(DC ...
- 论文阅读: End-to-end Learning of Action Detection from Frame Glimpses in Videos
End-to-End Learning of Action Detection from Frame Glimpses in Videos CVPR 2016 Motivation: 本 ...
- 【Python】【有趣的模块】tqdm | inspect
tqdm """ [tqdm] 显示循环的进度条,再也不用担心程序跑到哪里还要跑多久了 tqdm 可以直接包裹iterable对象 from tqdm import tq ...
- _itemmod_creation_enchant
该表控制物品产生时自动获得随机附魔效果 comment 备注 entry 物品entry slot 附魔位置1-5,这些位置都可以用来产生自带附魔效果 groupId 附魔组Id 对就_ ...
- 安装ik分词器以及版本和ES版本的兼容性
一.查看自己ES的版本号与之对应的IK分词器版本 https://github.com/medcl/elasticsearch-analysis-ik/blob/master/README.md 二. ...