LeetCode:杨辉三角【118】
LeetCode:杨辉三角【118】
题目描述
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。
示例:
输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
题目分析
模拟杨辉三角的形成过程即可!
Java题解
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner; public class PascalsTriangle_118 {
public static List<List<Integer>> generate(int numRows) {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> line = new ArrayList<>();
if(numRows<1)
return ans;
line.add(1);
ans.add(line);
if(numRows>1)
{
for(int i = 2;i<=numRows;i++)
{
line = new ArrayList<>();
line.add(1);
List<Integer> lastLine = ans.get(ans.size()-1);
for(int j = 1;j<i-1;j++)
{
line.add(lastLine.get(j)+lastLine.get(j-1));
}
line.add(1);
ans.add(line);
}
}
return ans;
} public static void main(String[] args) {
generate(5);
} }
LeetCode:杨辉三角【118】的更多相关文章
- leetcode 杨辉三角
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2,1], [ ...
- LeetCode 118. 杨辉三角
118. 杨辉三角 给定一个非负整数numRows,生成杨辉三角的前numRows行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例 输入: 5 输出: [ [1], [1,1], [1,2 ...
- 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(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- LeetCode 118:杨辉三角 II Pascal's Triangle II
公众号:爱写bug(ID:icodebugs) 作者:爱写bug 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. Given a non-negative index k whe ...
- Java实现 LeetCode 118 杨辉三角
118. 杨辉三角 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], ...
- LeetCode(118):杨辉三角
Easy! 题目描述: 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1] ...
- leetcode 118. 杨辉三角(python)
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5输出:[ [1], [1,1], [1,2,1], [1, ...
- [leetcode]118,119PascalsTriangle,杨辉三角1,2
杨辉三角1Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,R ...
随机推荐
- 获取CPU个数
PROCESSER=`grep 'processor' /proc/cpuinfo | wc -l` JOBSS=$[$PROCESSER*2]
- 李洪强经典面试题41-iOS选择题
1.及时聊天app不会采用的网络传输方式是 DA UDP B TCP C Http D FTP 2.下列技术不属于多线程的是 AA Block B NSThread C NSOperation D G ...
- 微信小程序注册开发流程
开篇: 微信小程序 很多刚学的同学都不太清楚如何去申请这个小程序的appid 现在呢我就一步步的告诉大家这个流程: 首先第1步,百度搜索:微信公众平台-点击右上角的立即注册 第2步:可以看到有4大注册 ...
- symbol lookup error
今天编译代码时出现这样的错误提示: “./test: symbol lookup error: ./test: undefined symbol: ……” 问题原因是:test使用的动态库和makef ...
- C++注释规范
1 源文件头部注释 列出:版权.作者.编写日期和描述. /************************************************* Copyright:bupt Auth ...
- How tomcat works学习笔记
最近在看Tomcat的源码, 所以找了一本相关的书籍<How tomcat works>. 博客内容多为 学习该书时所记录的笔记.(如有侵权行为,请联系我:eviltomorrow@163 ...
- WPF-数据转换
有时我们展现的数据,需要进行转换,比如如果一个学生的成绩过了60,我们显示一个Pass的图片. XAML: <Window x:Class="DeepXAML.MainWindow&q ...
- 一套一般般的前端框架 layui
http://www.layui.com/doc/
- HDOJ 4549 M斐波那契数列 费马小定理+矩阵高速幂
MF( i ) = a ^ fib( i-1 ) * b ^ fib ( i ) ( i>=3) mod 1000000007 是质数 , 依据费马小定理 a^phi( p ) = 1 ( ...
- sublime text3 安装配置
sublime text 3 语法检查插件(一直都是安装了但是却没有语法报错提示和苦恼) 第一种方法:有点卡 先去下载对应的开发环境,安装到本地,例如php. 从Pakage Control中安装su ...