当前位置:网站首页>Leetcode 笔记 118. 杨辉三角
Leetcode 笔记 118. 杨辉三角
2022-07-28 12:07:00 【Lyrig~】
题目描述
给定一个非负整数 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
思路
就是模拟,注意递推关系 a n s [ i ] [ j ] = a n s [ i − 1 ] [ j − 1 ] + a n s [ i − 1 ] [ j ] ( j ! = 0 ∣ ∣ i ) a n s [ i ] [ j ] = 1 ( j = 0 ∣ ∣ i ) \mathcal{ans[i][j]} = ans[i - 1][j - 1] +ans[i-1][j](j!=0 ||i)\\ ans[i][j]=1(j = 0 ||i) ans[i][j]=ans[i−1][j−1]+ans[i−1][j](j!=0∣∣i)ans[i][j]=1(j=0∣∣i)
其中ans[i][j]表示杨辉三角第i行第j个
代码
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> ans;
for(int i = 0; i < numRows; ++ i)
{
ans.push_back(vector<int>(i + 1));
for(int j = 0; j < i + 1; ++j)
{
if(j == 0 || j == i)
{
ans[i][j] = 1;
}
else ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j];
}
}
return ans;
}
};
边栏推荐
- Complete set of SSM framework online bookstore
- 10、 Kubernetes scheduling principle
- Array, string de duplication
- Fundamentals of machine learning - principal component analysis pca-16
- Is jetpack compose completely out of view?
- 【嵌入式C基础】第7篇:C语言流程控制详讲
- Understanding of vite2
- Brief introduction to JS operator
- The difference between sessionstorage, localstorage and cookies
- Fundamentals of machine learning Bayesian analysis-14
猜你喜欢

我抄底了被清算的NFT,却被OpenSea上了锁

With 433 remote control UV lamp touch chip-dlt8sa20a-jericho

Le transaction

Change the document type in endnode and import it in word
![[graduation design teaching] ultrasonic ranging system based on single chip microcomputer - Internet of things embedded stm32](/img/27/58fd175753b21dc21bd2d950cf5f15.png)
[graduation design teaching] ultrasonic ranging system based on single chip microcomputer - Internet of things embedded stm32

机器学习基础-集成学习-13

How to design a second kill system?

How to open the power saving mode of win11 system computer

SQL most commonly used basic operation syntax

【嵌入式C基础】第2篇:进制转换与BCD编码
随机推荐
The difference between sessionstorage, localstorage and cookies
Definition of option basis
Summary: golang's ide:vscode usage
Protobuf data exchange format
JVM 内存管理 你知道多少
LeetCode每日一题(2196. Create Binary Tree From Descriptions)
【嵌入式C基础】第4篇:运算符的使用
Form for real-time custom verification
机器学习基础-集成学习-13
Margin calculation
Databinding+LiveData轻松实现无重启换肤
Li FuPan: application practice of kata safety container in ant group
Use and source code of livedata in jetpack family bucket
[embedded C foundation] Part 2: binary conversion and BCD coding
Code layered management of interface testing based on RF framework
Ruan Bonan of Green Alliance Technology: cloud native security from the open source shooting range
How many times can the WordPress user name be changed? Attach the method of changing user name
Jetpack Compose 完全脱离 View 系统了吗?
《TiDB 6.x in Action》发布,凝聚社区集体智慧的 6.x 实践汇总!
【嵌入式C基础】第8篇:C语言数组讲解