[leetcode-118]Pascal's triangle 杨辉三角
Pascal's triangle
(1过)
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]
]
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。
示例:
输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
public class PascalTriangle {
public ArrayList<ArrayList<Integer>> generate(int numRows) {
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
if (numRows <= 0) {
return res;
}
for (int i=0;i<numRows;i++) {
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
for (int j=1;j<=i-1;j++) {
list.add(res.get(i-1).get(j-1) + res.get(i-1).get(j));
}
if (i>=1) {
list.add(1);
}
res.add(list);
}
return res;
}
}
pascals-triangle-ii
给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。
示例:
输入: 3
输出: [1,3,3,1]
进阶:
你可以优化你的算法到 O(k) 空间复杂度吗?
public ArrayList<Integer> getRow(int rowIndex) {
ArrayList<Integer> res = new ArrayList<>();
if (rowIndex < 0) {
return res;
}
res.add(1);
for (int i=0;i<=rowIndex;i++) {
// 关键点,从后往前遍历,从前往后的话set(j)会覆盖掉set(j+1)需要的上一行的j
for (int j=i-1;j>0;j--) {
res.set(j,res.get(j-1) + res.get(j));
}
if (i>=1) {
res.add(1);
}
}
return res;
}
}
[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 ...
- 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- ...
随机推荐
- WEB请求处理
https://blog.csdn.net/bpingchang/article/details/51328941
- android 通过修改图片像素实现CircleImageView
CircleImageView实现方法有很多种,各有优缺点,因此需要按照不同的场景使用.我们今天使用修改图片像素的方法实现CircleImageView,主要知识点无非是勾股定理和点到圆形的距离. 素 ...
- 自学Python4.2-装饰器
自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...
- [luogu5253]丢番图【数学】
传送门 [传送门] 题目大意 求\(\frac{1}{x}+\frac{1}{y}=\frac{1}{n}\)有多少组不同的解. 分析 将式子转化成\((n-x)(n-y)=n^2\)的形式. 那么很 ...
- NOIP2018保卫王国
题目大意:给一颗有点权的树,每次规定两个点选还是不选,求这棵树的最小权点覆盖. 题解 ZZ码农题. 要用动态dp做,这题就是板子,然鹅并不会,留坑代填. 因为没有修改,所以可以静态倍增. 我们先做一遍 ...
- [ZJOI2010]贪吃的老鼠(网络流+建图)
题目描述 奶酪店里最近出现了m只老鼠!它们的目标就是把生产出来的所有奶酪都吃掉.奶酪店中一天会生产n块奶酪,其中第i块的大小为pi,会在第ri秒被生产出来,并且必须在第di秒之前将它吃掉.第j只老鼠吃 ...
- 牛客小白月赛12C (线性筛积性函数)
链接:https://ac.nowcoder.com/acm/contest/392/C来源:牛客网 题目描述 华华刚刚帮月月完成了作业.为了展示自己的学习水平之高超,华华还给月月出了一道类似的题: ...
- Ubuntu 18.04 安装微信(Linux通用)
Linux相关的知识:https://www.cnblogs.com/dunitian/p/4822808.html#linux 新增谷歌浏览器添加到桌面的彻底删除:https://www.cnblo ...
- Java 破解谷歌翻译api,可以实现程序自动化翻译文章
1 原理:查看谷歌翻译网站,输入需要翻译的文字,选择语言得到翻译后的文字,发送异步请求参数返回结果.java使用httpclient发送请求,实现使用代码翻译文章的功能. 2 下载代码后,测试入口 ...
- redis设置最大内存