当前位置:网站首页>LeetCode 0118. Yanghui triangle
LeetCode 0118. Yanghui triangle
2022-07-19 13:03:00 【Tisfy】
【LetMeFly】118. Yang hui triangle
Force button topic link :https://leetcode.cn/problems/pascals-triangle/
Given a nonnegative integer numRows, Generate 「 Yang hui triangle 」 Before numRows That's ok .
stay 「 Yang hui triangle 」 in , Each number is the sum of the numbers at the top left and right of it .

Example 1:
Input : numRows = 5 Output : [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Example 2:
Input : numRows = 1 Output : [[1]]
Tips :
1 <= numRows <= 30
Method 1 : Dynamic programming
We use it vector<vector<int>> ans To store Yang Hui triangle .
initialization The first line of Yang Hui triangle (ans.push_back({1}))
And then from 2 2 2 OK, let's start ( Subscript i i i from 1 1 1 Start ), Constantly generate each line .
The first i + 1 The generation method of rows is :
First add the first element to this line 1 1 1(ans.push_back({1}))
And then from the 2 2 2 Elements start ( Subscript j j j from 1 1 1 Start ), Until the previous element of the last element in this line , The first i + 1 OK, No j + 1 Elements are equal to the sum of the corresponding two elements in the previous line (ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j])
Last , Add another... To the end of this line 1 1 1 that will do .
If you don't understand , You can refer to the chapter of Yang Hui triangle 3 3 3 OK, No 2 2 2 Elements 3 3 3 To study :
When calculated to the third 3 3 3 OK, No 2 2 2 Element time , i = 2 , j = 1 i=2,j=1 i=2,j=1. here a n s [ i ] [ j ] = a n s [ i − 1 ] [ j − 1 ] + a n s [ i − 1 ] [ j ] = 1 + 1 = 2 ans[i][j]=ans[i-1][j-1]+ans[i-1][j]=1+1=2 ans[i][j]=ans[i−1][j−1]+ans[i−1][j]=1+1=2, Note No 3 3 3 OK, No 2 2 2 The value of each element is 2 2 2.

- Time complexity O ( s i z e 2 ) O(size^2) O(size2)
- Spatial complexity O ( 1 ) O(1) O(1), The answer is not included in the algorithm space complexity
AC Code
C++
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> ans;
ans.push_back({
1});
for (int i = 1; i < numRows; i++) {
ans.push_back({
1});
for (int j = 1; j < i; j++) {
ans[i].push_back(ans[i - 1][j - 1] + ans[i - 1][j]);
}
ans[i].push_back(1);
}
return ans;
}
};
Running results : It takes quite a little time

Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125829159
边栏推荐
- Nombre minimal d'échanges
- Redis logical cluster creation
- Possible problems in inserting Excel data into MySQL database
- 【Try to Hack】arp和arp欺骗
- LeetCode_ Prefix and_ Medium_ 523. Continuous subarrays and
- Harmonyos quick start: Hello World
- Summary of ultrasonic sensor series articles
- 又错了,字节对齐及#pragma pack的使用
- Metal organic framework / nitrogen carbide nano sheet (uio-66/hocn) composite | mil-101 loaded Au Pd alloy nanoparticles | chemical reagent MOF customization
- When will the moon appear
猜你喜欢

Is the career direction of test / development programmers over 35 a turning point in the workplace?

关于TCP/IP协议漏洞的安全措施

学习记录:调用TFTLCD液晶屏

AI is the designer who knows you best? Let users generate digital clothing "by heart" Adidas ozworld

When will the deflationary market reverse? How should we operate? 2020-03-13

使用case语句时会产生锁存器的情况

Ultrasonic sensor (ch101 & ch201) - I

XML建模(简单易学)

关于“抄底”心态,让你筋疲力尽 2020-03-15

市场“不确定性”中的投资逻辑 2020-03-18
随机推荐
XML modeling (easy to learn)
R语言--Cox模型校准曲线原理(一)数据来源
Impulse function, step function, ramp function and impulse response
通货收缩的市场何时反转?我们该如何操作?2020-03-13
Flask源码分析(三):上下文
go web
Redis集群主备缓存区满了导致主备频繁倒换
Knowledge sorting of MySQL
Return to risk ratio: the most important indicator of investment opportunities 2020-03-14
Ossimport migration path
音频控制常见BUG注意事项
Audio common terminal anatomy - never make a mistake again
XML建模(简单易学)
S32K148_ Can drive (bare metal development)
可视化ETL工具Kettle概念、安装及实战案例
又错了,字节对齐及#pragma pack的使用
Do you still need to release the database connection manually with typeorm
Lazy to the bone, I am too lazy to write articles in CSDN. I write articles based on selenium simulation
Amino metal organic framework material Fe MOF, fe-mil-88nh2 | Zr based metal organic framework catalyst (pt-uio-66) | Qiyue biology
Fluorine modified uio-66 | 3,4-dihydroxybenzaldehyde modified uio-66-nh2 | camptothecin derivative / oligopeptide @zif-8 nano drug carrier system