当前位置:网站首页>118. Yanghui triangle
118. Yanghui triangle
2022-06-26 10:09:00 【later_ rql】
118. Yang hui triangle
Title Description
Their thinking
Solution 1 : mathematics
Ideas : There are two layers , Outermost list ret And the elements in the list ( It's also a list )row. then , For each line of Yang Hui triangle , On both sides 1(j0 || j1), The middle element , Use the previous line to correspond to the sum of the two elements (ret.get(i-1).get(j-1)+ret.get(i-1.get(j))). Last , Add each row to ret in .
Time complexity :O(numRows^2).
Spatial complexity :O(1). Regardless of the space occupation of the return value
Code
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;
}
}
link :https://leetcode-cn.com/problems/pascals-triangle/solution/yang-hui-san-jiao-by-leetcode-solution-lew9/
source : Power button (LeetCode).
边栏推荐
- Deep learning (tentsorflow2. version) three good student performance problems (1)
- 【Leetcode】76. 最小覆盖子串
- 首批12家企业入驻!广州首个集中展销老字号产品专柜开张
- Teach you to use shell script to check whether the server program is running
- Internationalization configuration
- Mysql database operation commands (constantly updated)
- libgstreamer-1.0. so. 0: cannot open shared object file: No such file or directory
- Do you know the //go: instructions in the go source code, go:linkname?
- SQL advanced tutorial
- LeetCode 0710. Random numbers in the blacklist - preprocessing implementation o (1) value
猜你喜欢
exec系列函数(execl、execlp、execle、execv、execvp)使用
Internationalization configuration
Redis notes (12) - single thread architecture (non blocking IO, multiplexing) and multiple asynchronous threads
C中字符串基本操作
做测试需要知道的内容——url、弱网、接口、自动化、
WGCLOUD的web ssh服务端口是多少
Software testing - how to select the appropriate orthogonal table
Cloud native essay using Hana expression database service on Google kubernetes cluster
字符串常量池、class常量池和运行时常量池
WIN10系统实现Redis主从复制
随机推荐
Does the go compiled executable have dynamic library links?
What you need to know to test -- URL, weak network, interface, automation
Glide's most common instructions
Teach you to use shell script to check whether the server program is running
WIN10系统实现Redis主从复制
c语言语法基础之——局部变量及存储类别、全局变量及存储类别、宏定义 学习
Druid data source for background monitoring
LeetCode 接雨水系列 42.(一维) 407.(二维)
My creation anniversary
Redis novice introduction
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)
SQL function
The basis of C language grammar -- function nesting, Fibonacci sum of recursive applet and factorial
The basis of C language grammar -- function definition learning
Leetcode connected to rainwater series 42 (one dimension) 407 (2D)
libgstreamer-1.0. so. 0: cannot open shared object file: No such file or directory
字符串常量池、class常量池和运行时常量池
Today's headline adaptation scheme code
自动化测试——pytest本身及第三方模块介绍及使用
Cloud native essay using Hana expression database service on Google kubernetes cluster