当前位置:网站首页>LeetCode 0118. 杨辉三角
LeetCode 0118. 杨辉三角
2022-07-17 17:15:00 【Tisfy】
【LetMeFly】118.杨辉三角
力扣题目链接:https://leetcode.cn/problems/pascals-triangle/
给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。
在「杨辉三角」中,每个数是它左上方和右上方的数的和。

示例 1:
输入: numRows = 5 输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
示例 2:
输入: numRows = 1 输出: [[1]]
提示:
1 <= numRows <= 30
方法一:动态规划
我们用vector<vector<int>> ans来存放杨辉三角。
初始化杨辉三角的第一行(ans.push_back({1}))
之后从第 2 2 2行开始(下标 i i i从 1 1 1开始),不断生成每一行。
第i + 1行的生成方法为:
首先向这一行中添加第一个元素 1 1 1(ans.push_back({1}))
然后从第 2 2 2个元素开始(下标 j j j从 1 1 1开始),到这一行最后一个元素的前一个为止,第i + 1行的第j + 1个元素等于上一行的对应两个元素之和(ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j])
最后,再向这一行的末尾添加一个 1 1 1即可。
如果不理解,可以参考杨辉三角的第 3 3 3行的第 2 2 2个元素 3 3 3的生成来研究:
当计算到第 3 3 3行的第 2 2 2个元素时, i = 2 , j = 1 i=2,j=1 i=2,j=1。此时 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,说明第 3 3 3行的第 2 2 2个元素的值为 2 2 2。

- 时间复杂度 O ( s i z e 2 ) O(size^2) O(size2)
- 空间复杂度 O ( 1 ) O(1) O(1),答案不计入算法空间复杂度
AC代码
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;
}
};
运行结果:耗时还挺少

同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/125829159
边栏推荐
- 光大期货网上开户安全吗?有没有开户指引?
- 竞赛笔记:Numpy学习笔记
- 2022 safety officer-c certificate title and answer
- 整理了一份通用的内存管理驱动代码
- Att & CK actual combat series - red team actual combat (-)
- Ultrasonic sensor (ch101 & ch201) - I
- 【Try to Hack】arp和arp欺骗
- 减半行情会不会来?有何投资机会?2020-03-11
- 最懂你的服装设计师是AI?让用户 “凭心意” 生成数字服装#Adidas OZWORLD
- 506. Relative ranking
猜你喜欢

【Pygame 学习笔记】6.Cursor 鼠标光标

Label ball problem

Yunxi and Tencent cloud have reached a strategic cooperation to accelerate the expansion of the global live broadcast market

Acwing4405. 统计子矩阵

Impulse function, step function, ramp function and impulse response
[email protected] Nanocomposites | zif-8/ poly"/>MOF customized materials | bimetallic CuNi MOF nano materials | core-shell structure [email protected] Nanocomposites | zif-8/ poly

Preparation Notes: Matplotlib learning notes a

Return to risk ratio: the most important indicator of investment opportunities 2020-03-14

VMware导入ova/ovf虚拟机文件

35岁以上的测试/开发程序员职业生涯走向,是职场转折点吗?
随机推荐
【Pygame 学习笔记】5.rect对象的碰撞检测
GO 单元测试
MOF customized product | n-k2ti4o9/g-c3n4/uio-66 ternary composite | paper based au-aginse2-zif-8 Nanocomposite
Three minutes to understand the primary key, foreign key, non empty, unique and default constraints in mysql, and how to create a table
Brief analysis of circuit fault
Preparation Notes: Matplotlib learning notes a
Uio-66 - (COOH) 2 modified polyamide nanofiltration membrane | zif-8/pvp composite nanofiber membrane | uio-66-nh2 modified polyamide nanofiltration membrane
Interview difficulties: difficulties in implementing distributed session, this is enough!
Summary of ultrasonic sensor series articles
Investment logic in market "uncertainty" 2020-03-18
Database daily question --- day 25: bank account summary II
Common bug precautions of audio control
图执行引擎那些事(二)
R language -- principle of Cox model calibration curve (I) data source
The active and standby cache of redis cluster is full, causing frequent active and standby switchover
FFmpeg转换视频格式与导出GIF动态图的方法
Metal organic framework / nitrogen carbide nano sheet (uio-66/hocn) composite | mil-101 loaded Au Pd alloy nanoparticles | chemical reagent MOF customization
Redis logical cluster creation
备赛笔记:matplotlib学习笔记a
Cadmium sulfide supported mil-125 (TI) | streptavidin (SA) - zirconium porphyrin MOF composite (PCN- [email protected] )|Shell core