当前位置:网站首页>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).
边栏推荐
- Tensorflow dynamically allocates video memory
- Redis notes (14) - persistence and data recovery (data persistence RDB and AOF, data recovery, mixed persistence)
- 测试实践——app 测试注意点
- 逻辑英语结构【重点】
- 【深度优先搜索】312.戳气球
- Crawler related articles collection: pyppeter, burpsuite
- 字符串常量池、class常量池和运行时常量池
- Redis notes (16) - info instructions and command line tools (view memory, status, number of client connections, monitoring server, scan large keys, sampling server, execute batch commands, etc.)
- Cento7.7 elk installation simple record
- 存储过程测试入门案例
猜你喜欢

字符串常量池、class常量池和运行时常量池

c语言语法基础之——指针( 多维数组、函数、总结 ) 学习

cmake / set 命令

c语言语法基础之——指针(字符、一维数组) 学习

2021 national vocational college skills competition (secondary vocational group) network security competition questions (1) detailed analysis tutorial

DAY 3 数组,前置后置,字符空间,关键词和地址指针

Full introduction to flexboxlayout (Google official flexible implementation of flow layout control)

install realsense2: The following packages have unmet dependencies: libgtk-3-dev

方法区里面有什么——class文件、class文件常量池、运行时常量池

exec系列函数(execl、execlp、execle、execv、execvp)使用
随机推荐
Solution to network request crash in retrofit2.8.1
TensorFlow遇到的各种错误
What you need to know to test -- URL, weak network, interface, automation
c语言语法基础之——局部变量及存储类别、全局变量及存储类别、宏定义 学习
P1296 whispers of cows (quick row + binary search)
Throttling, anti chattering, new function, coriolism
国际化配置
From TF 1 X to TF 2.6 (update if you encounter it)
自动化测试——pytest本身及第三方模块介绍及使用
Opencv depthframe - > pointcloud causes segmentation fault!
Redis master-slave replication in win10 system
logback
美国总统签署社区安全法案以应对枪支问题
What is the web SSH service port of wgcloud
install ompl. sh
Speed test of adding, deleting, modifying and querying 5million pieces of data in a single MySQL table
字符串常量池、class常量池和运行时常量池
LeetCode 958. Completeness checking of binary tree
测试须知——常见接口协议解析
Go learning notes (83) - code specification and common development skills