当前位置:网站首页>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).
边栏推荐
- Redis novice introduction
- Custom interceptor
- Daily-used English phrases
- 2. 合并两个有序数组
- MySQL learning summary
- The basis of C language grammar -- learning of local variables and storage categories, global variables and storage categories, and macro definitions
- P1296 whispers of cows (quick row + binary search)
- Code statistics tools cloc and SCC
- Full introduction to flexboxlayout (Google official flexible implementation of flow layout control)
- TensorFlow动态分配显存
猜你喜欢

Basic grammar of C language -- pointer (character, one-dimensional array) learning

jar版本冲突问题解决

Install new version cmake & swig & tinyspline

Force buckle ----- remove the maximum and minimum values from the array

mysql学习总结

logback

#云原生征文# 在 Google Kubernetes Cluster 上使用 HANA Expression Database Service

Cloud native essay using Hana expression database service on Google kubernetes cluster

自动化测试——pytest本身及第三方模块介绍及使用

定制拦截器
随机推荐
P1296 whispers of cows (quick row + binary search)
This new change of go 1.16 needs to be adapted: the changes of go get and go install
國際化配置
Vscode common programming fonts
MySQL learning summary
调用api接口生成不同颜色的微信小程序二维码
Detailed explanation of the network security competition questions (2) of the 2021 national vocational college skills competition (secondary vocational group)
2021 national vocational college skills competition (secondary vocational group) network security competition questions (1) detailed analysis tutorial
What is the web SSH service port of wgcloud
Redis master-slave replication in win10 system
【深度优先搜索】312.戳气球
软件测试---如何选择合适的正交表
Notes on sports planning on November 22, 2021
JSP file syntax
Deep learning (tentsorflow2. version) three good student performance problems (1)
Dialog centered
全渠道、多场景、跨平台,App如何借助数据分析渠道流量
JVM垃圾回收什么情况会进入老年代
118. 杨辉三角
Control setting layout in linear layout_ Gravity doesn't work?