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- ...
随机推荐
- 动态样式语言—LESS基础知识
CSS是一门非程序式语言,缺少逻辑性,不便于维护 LESS在CSS现有语法的基础上,为CSS加入程序式语言的特性 引入了变量.混入.运算.函数等功能,大大简化CSS的编写,降低了CSS的维护成本 LE ...
- 设计模式--桥接(Bridge)模式
1.概述: 桥接模式:把抽象和行为分离开来,中间需要某个介质来连接抽象化和行为化.此模式的概述听起来非常像适配器模式,不要搞混了,虽然都是借用中间介质,但意义不同.最典型的桥接模式就是:JDBC.通过 ...
- grovvy生成随机汉字名字
StringBuilder sb = new StringBuilder(); for(int i = 0 ;i < 3; i++){ sb.append((char) (0x4e00 + (i ...
- Word 测试下发布博客
目录 语法. 3 NULL,TRUE,FALSE 3 大小端存储 4 类型转换 4 转义字符 5 运算符的优先级 5 表达式(a=b=c) 7 *pa++=* ...
- python爬虫之Cookie
由于http协议是无状态协议(假如登录成功,当访问该网站的其他网页时,登录状态消失),此时,需要将会话信息保存起来,通过cookie或者session的方式 cookie 将所有的回话信息保存在客户端 ...
- 《Apache Kafka 实战》读书笔记-认识Apache Kafka
<Apache Kafka 实战>读书笔记-认识Apache Kafka 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.kafka概要设计 kafka在设计初衷就是 ...
- JAVA核心技术I---JAVA基础知识(文本文件读写)
一:java IO包概述 (一)Java读写文件,只能以(数据)流的形式进行读写 (二)java IO 包 –节点类:直接对文件进行读写 –包装类 • 转化类:字节/字符/数据类型的转化类 • 装饰类 ...
- JavaScript深度克隆(递归)
今天在深度理解JQuery源码时,剖析extend时: jQuery.extend = jQuery.fn.extend = function() { //... } 感觉该方法的一部分功能与深度克隆 ...
- Python复习笔记(七)线程和进程
1. 多任务 并行:真的多任务 并发:假的多任务 2. 多任务-线程 Python的 Thread模块是比较底层的模块,Python的 Threading模块 是对Thread做了一些包装,可以更加方 ...
- Openresty 学习笔记(四)lualocks包管理器安装使用
Luarocks是一个Lua包管理器,基于Lua语言开发,提供一个命令行的方式来管理Lua包依赖.安装第三方Lua包等,社区比较流行的包管理器之一,另还有一个LuaDist,Luarocks的包数量比 ...