当前位置:网站首页>LeetCode118. 杨辉三角
LeetCode118. 杨辉三角
2022-07-25 07:03:00 【学代码的咸鱼】
通过学习了vector,我们可以通过一些OJ练习题来增强学习vector的使用。

难度 简单OJ链接
首先,杨辉三角其实类似一个二维数组。但是里面的数据是不规则的,第一行一个数据,第二行两个数据,第三行三个数据… …其余空白的都是0,而第一列的数和斜着的数都为1,中间的数据是由左上角的数+右上角的数。
然后,我们看一下这个题目给我们的返回值是什么意思?
我们应该知道vector< int >的意思是:一个数组里存放的是int类型的数据。
而vector<vector< int >>的意思是:一个数组里存放的是vector< int >对象,每个对象里是一个数组存放的是int类型的数据。
这就是这个返回值的意思。那么现在有一个vector<vector< int >>类型的对象vv,那么我们该如何去遍历它呢?
我们可以这样来遍历。
vv.size()的意思是:vector<vector< int >>里的对象有多少个,也就是二维数组(杨辉三角)的行数。
vv[i]的意思是:每一行的那个管理int的vector。
vv[i].size()的意思是:每个vecto(行)r里有多少个int。
vv[i][j]的意思是:通过两次operator[]的调用,像访问二维数组一样访问数据。访问第i行的,第j个数据。本质上是先访问第i个vector< int >对象,再访问这个对象的第j个int数据。
然后,我们再来写这道题:
这里,一开始用的是resize函数而不能是reserve函数。这是因为我们先要初始化。如果用的是reserve函数,那么里面的有效值size是0,后面也不能对它进行使用,所以会开辟不出空间。如果我们用的是resize,那么就会默认初始化。
上面这是第一步,当我们把空间初始化都弄好时,就开始弄中间的数。而中间的数=由左上角的数+右上角的数。由于我们之前初始化的时候其它位置都置为1了,现在只需要搞0就行了。
这种遍历,我们上面也说过了。赋完值之后,我们把定义的这个对象返回就行了。
如果,大家觉得这道题对你有帮助。希望大家能够多多点赞。
边栏推荐
- labelme标注不同物体显示不同颜色以及批量转换
- [Yugong series] July 2022 go teaching course 015 assignment operators and relational operators of operators
- Cointegraph wrote: relying on the largest Dao usdd to become the most reliable stable currency
- Mathematics Olympiad vs Informatics Olympiad (July 19, 2022)
- 不只是日志收集,项目监控工具Sentry的安装、配置、使用
- JS data type judgment - Case 6 delicate and elegant judgment of data type
- Kyligence Li Dong: from the data lake to the index middle stage, improve the ROI of data analysis
- Rambus announces ddr5 memory interface chip portfolio for data centers and PCs
- [daily question] sword finger offer II 115. reconstruction sequence
- Dynamic memory management
猜你喜欢

Download and run the C open source control metroframework demo project

分层强化学习综述:Hierarchical reinforcement learning: A comprehensive survey

Not only log collection, but also the installation, configuration and use of project monitoring tool sentry

%d,%s,%c,%x

Robot engineering - teaching quality - how to judge

Rambus announces ddr5 memory interface chip portfolio for data centers and PCs

30 times performance improvement -- implementation of MyTT index library based on dolphin DB

Create a new STM32 project and configure it - based on registers
![[Yugong series] July 2022 go teaching course 015 assignment operators and relational operators of operators](/img/37/4a892b96bec8cfa7efe38046c5dfc9.png)
[Yugong series] July 2022 go teaching course 015 assignment operators and relational operators of operators

How to learn C language?
随机推荐
微信小程序switchTab传参以及接收参数
Get all file names of the current folder
labelme标注不同物体显示不同颜色以及批量转换
睡眠不足有哪些危害?
机器人工程-教学品质-如何判定
New tea, start "fighting in groups"
A little consideration of strategic mode
【C】 Program environment and pretreatment
Wechat applet wx.request interface
Ant design input search box listens for allowclear event separately
JS array = number assignment changes by one, causing the problem of changing the original array
The income of bank financial management is getting lower and lower. Now which financial products have high income?
knapsack problem
[OBS] DTS sent by video packet_ USEC calculation
[cann training camp] play with the one-stop plan of cann target detection and recognition - learning notes 1 (initial experience)
%d,%s,%c,%x
ArgoCD 用户管理、RBAC 控制、脚本登录、App 同步
Wei Lai: what is the difference between multithreaded join and detach?
[semidrive source code analysis] [drive bringup] 39 - touch panel touch screen debugging
Decrypting numpy is a key difficulty in solving the gradient