Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。

在杨辉三角中,每个数是它左上方和右上方的数的和。
示例:
输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
思路
对任意的n>0有
f(1, n)=1,(n>0)
f(1, 2)=1,(n=2)
f(i,j) = f(i-1, j-1)+f(i, j-1),i>2,j>2
代码实现
package Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 118. Pascal's Triangle(杨辉三角)
* 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
*/
public class Solution118 {
public static void main(String[] args) {
Solution118 solution118 = new Solution118();
int numRows = 1;
List<List<Integer>> res = solution118.generate(numRows);
System.out.println(res);
}
/**
* 对任意的n>0有
* f(1, n)=1,(n>0)
* f(1, 2)=1,(n=2)
* f(i,j) = f(i-1, j-1)+f(i, j-1),i>2,j>2
*
* @param numRows
* @return
*/
public List<List<Integer>> generate(int numRows) {
if (numRows == 0) {
return Collections.emptyList();
}
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
List<Integer> sub = new ArrayList<>();
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
sub.add(1);
} else {
List<Integer> upSub = res.get(i - 1);
sub.add(upSub.get(j - 1) + upSub.get(j));
}
}
res.add(sub);
}
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 ...
- [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
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- ...
随机推荐
- 软件在 win7 上运行时显示乱码
一个用户反应后,我当时就蒙圈了,因为之前从未遇到过: 百度一下后,发现用户的这种情况比较特殊,从表面上看,[控制面板]和[注册表]相关项设置都正常,为什么还显示乱码呢? 到最后一步如果已经是(简体,中 ...
- windows 文件夹下所有文件名称
dir dir /b 没有其它信息 dir /b >>xxx.txt 保存到txt
- 图像处理之gamma校正
1 gamma校正背景 在电视和图形监视器中,显像管发生的电子束及其生成的图像亮度并不是随显像管的输入电压线性变化,电子流与输入电压相比是按照指数曲线变化的,输入电压的指数要大于电子束的指数.这说明暗 ...
- 做错的题目——this的指向
不管延不延时,setTimeout里面的function都是孤立的,并不属于哪一个对象,所以this只会指向全局
- 剑指Offer_编程题_8
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. class Solution { public: int jumpFloor(int number ...
- u-boot(二)makefile
目录 u-boot(二)makefile 引入 目录结构(1.1.6) 配置文件 目标 配置具体的单板 编译阶段 过程 链接入口 配置链接地址 附录 附录A:mkconfig解析 附录B 链接脚本 t ...
- python 发送邮件模板
##发送普通txt文件(与发送html邮件不同的是邮件内容设置里的type设置为text,下面代码为发送普通邮件的另一种方法) import smtplibimport stringfrom emai ...
- VSCode 配置
官网 https://code.visualstudio.com/ 便携化 Windows,Linux 在 Code.exe 所在目录创建 data 目录 macOS 在 Code.exe 所在目录创 ...
- C++回顾day02---<引用>---待补充
一:引用概念---引用就是为一个变量起一个别名 每个变量都是指向一块内存空间的标识,引用就是重新设置一个标识,但是这个标识还是指向同一个内存空间 和指针类似(其实引用本质就是使用了一个常指针 cons ...
- JAVA核心技术I---JAVA基础知识(知识回顾)
一:多态问题 class Father { public void hello() { System.out.println("Father says hello."); } } ...