每天一道LeetCode--118. 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:
ackage cn.magicdu; import java.util.ArrayList;
import java.util.List; public class _118_Pascal_Triangle { public List<List<Integer>> generate(int numRows) {
if (numRows < 0) {
return null;
}
List<List<Integer>> l1 = new ArrayList<>();
if (numRows >= 1) {
List<Integer> list = new ArrayList<>();
list.add(1);
l1.add(list);
}
if (numRows >= 2) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(1);
l1.add(list);
}
if (numRows >= 3) {
for (int i = 3; i <= numRows; i++) {
List<Integer> list = new ArrayList<>();
List<Integer> prev = l1.get(i - 2);
list.add(1);
for (int j = 2; j <= i - 1; j++) {
list.add(prev.get(j - 2) + prev.get(j - 1));
}
list.add(1);
l1.add(list);
} } return l1;
} }
每天一道LeetCode--118. Pascal's Triangle(杨辉三角)的更多相关文章
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [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(杨辉三角)
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- Pascal's Triangle(杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 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 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- 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- ...
随机推荐
- Linux下移植pjsip,使用QT开发
1.移植pjsip env:fedora14 arm-linuc-gcc:gcc version 4.5.1 (ctng-1.8.1-FA) #./configure \ CC=arm-linux-g ...
- FPGA静态时序分析——IO口时序(Input Delay /output Delay)
1.1 概述 在高速系统中FPGA时序约束不止包括内部时钟约束,还应包括完整的IO时序约束和时序例外约束才能实现PCB板级的时序收敛.因此,FPGA时序约束中IO口时序约束也是一个重点.只有约束正确 ...
- sqlite3 多线程和锁 ,优化插入速度及性能优化
一. 是否支持多线程? SQLite官网上的"Is SQLite threadsafe?"这个问答. 简单来说,从3.3.1版本开始,它就是线程安全的了.而iOS的SQLite ...
- uva11324 The Largest Clique --- 强连通+dp
给一个有向图G,求一个子图要求当中随意两点至少有一边可达. 问这个子图中最多含多少个顶点. 首先找SCC缩点建图.每一个点的权值就是该点包括点的个数. 要求当中随意两点可达,实际上全部边仅仅能同方向, ...
- MFC Windows程序设计源代码免费下载
本人近期在网上找到了<MFC Windows程序设计>第二版的书内程序的源代码,特意上传CSDN上面,供学习MFC的程序猿们免费下载. 源代码下载: http://download.csd ...
- Asp.Net MVC 3【URLs, Routing,and Areas】续
http://www.cnblogs.com/HuiTai/archive/2012/07/24/2597875.html 接着前面继续学习分享我们的路由. 现在我们把Global.asax文件里的R ...
- [AngularJS] ngModelController render function
ModelValue and ViewValue: $viewValue: Actual string value in the view. $modelValue: The value in the ...
- 详解MYSQL数据库密码的加密方式及破解方法
MYSQL加密方式:http://blog.csdn.net/listeningsea/article/details/8139641
- Helpers\Tags
Helpers\Tags The tags helper is a collection of useful methods: Tags::clean($data) Clean function to ...
- Eclipse配置C/C++开发环境
开发环境:Eclipse3.2.CDT3.1.MinGW5.1 1.Eclipse及CDT的安装到Eclipse的官方网站http://www.eclipse.org上下载Eclipse.安装CDT. ...