当前位置:网站首页>118. 杨辉三角
118. 杨辉三角
2022-06-26 09:36:00 【later_rql】
题目描述

解题思路
解法一:数学
思路:分两层,最外层的列表ret和列表内元素(也是列表)row。然后,对于杨辉三角的每一行,两边均为1(j0 || j1),中间的元素,利用上一行对应两个元素之和(ret.get(i-1).get(j-1)+ret.get(i-1.get(j)))。最后,再将每一行添加到ret中。
时间复杂度:O(numRows^2)。
空间复杂度:O(1)。不考虑返回值的空间占用
代码
public static List<List<Integer>> generate(int numRows) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
for (int i = 0; i < numRows; ++i) {
List<Integer> row = new ArrayList<Integer>();
for (int j = 0; j <= i; ++j) {
if (j == 0 || j == i) {
row.add(1);
} else {
row.add(ret.get(i - 1).get(j - 1) + ret.get(i - 1).get(j));
}
}
ret.add(row);
}
return ret;
}
}
链接:https://leetcode-cn.com/problems/pascals-triangle/solution/yang-hui-san-jiao-by-leetcode-solution-lew9/
来源:力扣(LeetCode)。
边栏推荐
- libmagic 介绍
- thymeleaf中抽取公共片段
- Do you know the //go: instructions in the go source code, go:linkname?
- Nested recyclerview in nestedscrollview automatically slides to the bottom after switching
- Software testing - how to select the appropriate orthogonal table
- 微软 Edge 浏览器 IE 模式标签页出现卡死情况,已通过回滚更新修复
- How to find and install the dependent libraries of Debian system
- Differences between VI and vim and common commands
- 我的创作纪念日
- WGCLOUD的web ssh服务端口是多少
猜你喜欢

2021-11-29 quintic polynomial of trajectory planning

力扣------从数组中移除最大值和最小值

逻辑英语结构【重点】

Develop current learning objectives and methods

调用api接口生成不同颜色的微信小程序二维码

如何更改微信小程序二维码物料颜色

Logview Pro can be used if the log is too large

Install new version cmake & swig & tinyspline

Deep learning (tentsorflow2. version) three good student performance problems (1)

DAY 3 数组,前置后置,字符空间,关键词和地址指针
随机推荐
What you need to know to test -- URL, weak network, interface, automation
2021 national vocational college skills competition (secondary vocational group) network security competition questions (1) detailed analysis tutorial
Solution to network request crash in retrofit2.8.1
LeetCode 0710.黑名单中的随机数 - 预处理实现O(1)取值
Dialog centered
Cento7.7 elk installation simple record
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.npm ER
Code statistics tools cloc and SCC
SSM项目小例子,SSM整合图文详细教程
Halcon photometric stereoscopic
c语言语法基础之——函数嵌套、递归 小程序斐波那契之和、阶乘
Mysql database field query case sensitive setting
SQL 函数
Upgrade idea to 2021.2 shortcut keys
Specific meaning of go bootstrap
Constraintlayout control uses full Raiders
In the fragment, the input method is hidden after clicking the confirm cancel button in the alertdialog (this is valid after looking for it on the Internet for a long time)
thymeleaf中抽取公共片段
My creation anniversary
LeetCode 接雨水系列 42.(一维) 407.(二维)