当前位置:网站首页>Leetcode notes 118. Yang Hui triangle
Leetcode notes 118. Yang Hui triangle
2022-07-28 13:24:00 【Lyrig~】
Leetcode note 118. Yang hui triangle
Title Description
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
Ideas
It's simulation , Pay attention to recurrence relationship 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)
among ans[i][j] Means Yang Hui triangle No i Xing di j individual
Code
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;
}
};
边栏推荐
- Single option trading and cross trade
- 《TiDB 6.x in Action》发布,凝聚社区集体智慧的 6.x 实践汇总!
- Redis —— 基础篇
- 【嵌入式C基础】第8篇:C语言数组讲解
- Protobuf data exchange format
- Shell基础概念和变量
- Is jetpack compose completely out of view?
- Leetcode · daily question · 1331. array sequence number conversion · discretization
- gicv3 spi register
- Transaction of MySQL underlying principle (2)
猜你喜欢

Chinese translation of pointnet:deep learning on point sets for 3D classification and segmentation

Black cat takes you to learn EMMC Protocol Part 26: hardware reset operation of EMMC (h/w reset)

UV germicidal lamp chip dlt8p65sa Jericho

Single option trading and cross trade

MySQL 实践篇 —— 主从复制

Intrinsic value and time value of options

How to use databricks for data analysis on tidb cloud | tidb cloud User Guide

【嵌入式C基础】第2篇:进制转换与BCD编码

Kotlin是如何帮助你避免内存泄漏的?
![[报错]使用ssh登陆到另一台机器后,发现主机名还是自己|无法访问yarn8088](/img/81/641a5b3445534fc3b8c87ee6deaa64.png)
[报错]使用ssh登陆到另一台机器后,发现主机名还是自己|无法访问yarn8088
随机推荐
Aragon creates Dao polygon BSC test network
基于深度学习的超分辨率重建
Dimming and color matching cool light touch chip-dlt8ma12ts-jericho
[FPGA]: Joint Simulation of FPGA and MATLAB
UV germicidal lamp chip dlt8p65sa Jericho
Black cat takes you to learn EMMC protocol chapter 27: what is EMMC's dynamic capacity?
Leetcode-190. inverting binary bits
Sub thread update UI full solution
【嵌入式C基础】第4篇:运算符的使用
Low code: reduce technical capability requirements and improve software development efficiency
powerdesigner创建数据库模型(概念模型举例)
Kotlin是如何帮助你避免内存泄漏的?
为什么说Crypto游戏正在改变游戏产业?
How does the vditor renderer achieve server-side rendering (SSR)?
夜神模拟器抓包微信小程序
Shell基础概念和变量
Change password, confirm password verification antd
什么是事务及数据库的优化方法
什么叫杂谈(e网杂谈)
With 433 remote control UV lamp touch chip-dlt8sa20a-jericho