当前位置:网站首页>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;
}
};
边栏推荐
- leetcode-190.颠倒二进制位
- 【嵌入式C基础】第6篇:超详细的常用的输入输出函数讲解
- [July 5 event preview] Flink Summit
- FFT海浪模拟
- Auto.js enables Taobao to quickly submit orders
- 【嵌入式C基础】第3篇:常量和变量
- 2020-12-27
- Have a part of the game, after NFT is disabled in my world
- Use and source code of livedata in jetpack family bucket
- Pointnet++ Chinese Translation
猜你喜欢

Machine learning Basics - integrated learning-13

GameStop熊市杀入NFT交易,老牌游戏零售商借Web3焕发第二春

leetcode-136.只出现一次的数字

为什么说Crypto游戏正在改变游戏产业?

Aragon创建DAO polygon BSC测试网
![[embedded C foundation] Part 1: basic data types](/img/45/b0bc9e90b0582f0f2624ce27b5a76c.png)
[embedded C foundation] Part 1: basic data types

如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南

Chapter 6 promotion

Comments are not allowed in JSON

Led aquarium lamp touch chip-dlt8t02s-jericho
随机推荐
Complete set of SSM framework online bookstore
Bull spread portfolio
Using auto.js to realize fifaol3 brush teaching assistant
拥有游戏的一部分,写在我的世界禁用NFT之后
RGB game atmosphere light touch chip-dlt8s04a-jericho
Definition of option basis
Machine learning Basics - integrated learning-13
10、 Kubernetes scheduling principle
企业数字化本质
LeetCode每日一题(2196. Create Binary Tree From Descriptions)
Pointnet++ Chinese Translation
【嵌入式C基础】第3篇:常量和变量
Tidb 6.x in action was released, a summary of 6.x practices that condense the collective wisdom of the community!
2020-12-27
[FPGA] FIR filter - half band filter
Original juice multifunctional Juicer touch chip-dlt8t02s-jericho
Stepless dimming colorful RGB mirror light touch chip-dlt8s12a-jericho
Change password, confirm password verification antd
Android工程师,如何使用Kotlin提供生产力?
Aragon creates Dao polygon BSC test network