当前位置:网站首页>LeetCode 118、杨辉三角
LeetCode 118、杨辉三角
2022-04-22 12:17:00 【亡于灬】
118、杨辉三角
1)题目描述
给定一个非负整数 *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
2)分析
迭代。每一行两端的元素和中间的元素分开计算。每一行两端都是1,中间的元素迭代计算,使用前一行计算后一行。
- 杨辉三角的第
i行有i个元素; - 杨辉三角每一行的第一个元素和最后一个元素都是
1; - 杨辉三角第三行开始,非两端的元素
res[i][j]=res[i-1][j-1]+res[i-1][j];
3)C++代码
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> res(numRows);
for(int i=0;i<numRows;i++){
res[i].resize(i+1);
res[i][0]=res[i][i]=1;
for(int j=1;j<i;j++){
res[i][j]=res[i-1][j-1]+res[i-1][j];
}
}
return res;
}
};
版权声明
本文为[亡于灬]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38342510/article/details/124337729
边栏推荐
- 模糊集合论
- In depth analysis of the drawing source code process of view
- 【并发编程049】说说重排序的分类?
- [deeply understand tcallusdb technology] delete all data interface descriptions in the list - [list table]
- CPU和GPU有什么区别?
- 带你详细入门华为云会议【玩转华为云】
- 购买不同品牌的手机,怎么对比硬件配置?
- Research shows that young people are more and more reluctant to change their mobile phones. What are the hardware updates of the next generation of smart phones that will make you have the impulse to
- 31. Create two identical objects
- 离职的互联网人,都去哪儿了?
猜你喜欢
随机推荐
Oracle Data Guard和金仓KingbaseES集群的数据保护模式对比
日撸代码300行学习笔记 Day 47
Setting policy of thread pool size
Fee details page
What happens when you run the NPM install command?
Ner brief overview
Low frequency (LF) RFID intelligent terminal
What are the main mobile camera chips?
自动化测试的生命周期是什么?
The higher the refresh rate, the better?
在VSCode中设置滑动滚轮改变字体大小
通俗易懂地给女朋友讲:线程池的内部原理
Software intelligence: the AI service function of AAAS system: the relationship between the unconscious agent function of outline map and gossip map
【并发编程055】如下守护线程是否会执行finally模块中的代码?
Take you to Huawei cloud Conference [play with Huawei cloud]
MySQL 5.0安装教程图解详细教程
JS [detailed explanation] closure
论文阅读《Attention Concatenation Volume for Accurate and Efficient Stereo Matching》
Esp32-cam usage history
案例4-1.4:堆中的路径 (小顶堆的模拟建立和模拟路径)




![【深入理解TcaplusDB技术】扫描数据接口说明——[List表]](/img/ed/cccd5dee09d2f0a3e6c788bd265b36.png)



