当前位置:网站首页>剑指 Offer 31. 栈的压入、弹出序列
剑指 Offer 31. 栈的压入、弹出序列
2022-06-11 08:55:00 【拽拽就是我】
leetcode力扣刷题打卡
题目:剑指 Offer 31. 栈的压入、弹出序列
描述:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如,序列 {1,2,3,4,5} 是某栈的压栈序列,序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。
解题思路
1、把数字压入栈再判断;
原代码##
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int>s;
int ind = 0;
for (int i = 0; i < pushed.size(); ++i) {
s.push(pushed[i]);
while (!s.empty() && s.top() == popped[ind]) {
ind++;
s.pop();
}
}
return s.empty();
}
};
边栏推荐
- php 上传大文件 超过40秒 服务器500
- SAP abap 数据类型与数据对象
- Sword finger offer 62 The last remaining number in the circle
- What are precompiled, compiled, assembled, linked, static and dynamic libraries
- Supplementary provision plan codeworks round 760 (Div. 3)
- 【C语言-函数栈帧】从反汇编的角度,剖析函数调用全流程
- EN 45545-2:2020 T11 smoke toxicity test
- for in / for of / forEach 循环
- EN 45545-2:2020 T11烟毒性检测
- SAP ODATA 开发教程
猜你喜欢

Matlab学习8-图像处理之线性与非线性锐化滤波、非线性平滑滤波
![[Clickhouse column] user initialization of new library role](/img/00/a11fb1a8e38ed4e0634839160c8ead.png)
[Clickhouse column] user initialization of new library role

The leader asked me to rewrite the test code. Should I do the same?

Standardized compilation knowledge

剑指 Offer 40. 最小的k个数

Iso8191 test is mentioned in as 3744.1. Are the two tests the same?

复制的代码格式混乱怎么办?

leetcode - 739. 每日温度

C语言打印菱形

typescript高阶特性一 —— 合并类型(&)
随机推荐
Using docker compose to build redis5 cluster
The leader asked me to rewrite the test code. Should I do the same?
Why is the string class final decorated
leveldb简单使用样例
Standardized compilation knowledge
Livedata and stateflow, which should I use?
The interviewer asked four questions and summed up four experiences
Analysis of EN 45545 R24 oxygen index test method
EN 45545-2T10水平法烟密度检测的注意事项
Printf correlation of C
19. 删除链表的倒数第 N 个结点
PVC 塑料片BS 476-6 火焰传播性能测定
PVC plastic sheets BS 476-6 determination of flame propagation properties
php 上传大文件 超过40秒 服务器500
马志强:语音识别技术研究进展和应用落地分享丨RTC Dev Meetup
Hibernate L2 cache
木板ISO 5660-1 热量释放速率摸底测试
Not eligible for getting processed by all beanpostprocessors
multiplication table
Which Apple devices support this system update? See if your old apple device supports the latest system