当前位置:网站首页>946. 验证栈序列
946. 验证栈序列
2022-07-05 12:39:00 【厚积薄发ض】
给定 pushed 和 popped 两个序列,每个序列中的 值都不重复,只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop 操作序列的结果时,返回 true;否则,返回 false 。
示例 1:
输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1] 输出:true 解释:我们可以按以下顺序执行: push(1), push(2), push(3), push(4), pop() -> 4, push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
示例 2:
输入:pushed = [1,2,3,4,5], popped = [4,3,5,1,2] 输出:false 解释:1 不能在 2 之前弹出。
提示:
1 <= pushed.length <= 10000 <= pushed[i] <= 1000pushed的所有元素 互不相同popped.length == pushed.lengthpopped是pushed的一个排列
我们做法是先将进栈序列入栈,当出栈序列与入栈序列相同的时候,栈顶元素要出栈,同时出栈序列接着遍历下一个元素
这是相同的情况:

这是不同的情况:

思路:
让入栈队列一一入栈,看栈顶元素是否与出栈队列元素相同,如果不相同,依次让入栈队列一一入栈,如果相同就让栈顶元素出栈,同时出栈队列遍历下一个元素,继续往复循环,最后如果入栈数组遍历完成并且栈为空就证明入栈队列与出栈队列是相同的,如果最后入栈数组遍历完成而栈不为空,那就证明入栈队列与出栈队列不相同。
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Stack<Integer> s = new Stack<Integer>();
int index = 0;
for(int i = 0 ; i < pushed.length; i ++){
s.push(pushed[i]);//将入栈序列压栈
while(!s.isEmpty() && index<popped.length&&popped[index] == s.peek()){
//在栈不为空的情况下并且index不越界看出栈序列是否与栈顶元素相同
s.pop();//如果相同弹出栈顶元素
index++;//遍历下一个元素
}
}
return s.isEmpty();
}
}边栏推荐
- Full text search of MySQL
- [cloud native] event publishing and subscription in Nacos -- observer mode
- Setting up sqli lab environment
- SAP self-development records user login logs and other information
- 使用 jMeter 对 SAP Spartacus 进行并发性能测试
- 研究:数据安全工具在 60% 的情况下无法抵御勒索软件
- Transactions from January 14 to 19, 2022
- 太方便了,钉钉上就可完成代码发布审批啦!
- Annotation problem and hidden Markov model
- SAP UI5 FlexibleColumnLayout 控件介绍
猜你喜欢

From the perspective of technology and risk control, it is analyzed that wechat Alipay restricts the remote collection of personal collection code

Pinduoduo flag insertion remarks API

Distance measuring sensor chip 4530a used in home intelligent lighting

10 minute fitness method reading notes (3/5)

Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue

ActiveMQ installation and deployment simple configuration (personal test)

Resnet+attention project complete code learning

The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation

从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks

谈谈我写作生涯的画图技巧
随机推荐
【云原生】Nacos-TaskManager 任务管理的使用
SAP UI5 ObjectPageLayout 控件使用方法分享
stirring! 2022 open atom global open source summit registration is hot!
Taobao flag insertion remarks | logistics delivery interface
激动人心!2022开放原子全球开源峰会报名火热开启!
C alarm design
Halcon 模板匹配实战代码(一)
JSON parsing error special character processing (really speechless... Troubleshooting for a long time)
【Nacos云原生】阅读源码第一步,本地启动Nacos
ActiveMQ installation and deployment simple configuration (personal test)
Time conversion error
Four common problems of e-commerce sellers' refund and cash return, with solutions
上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花
A few years ago, I outsourced for four years. Qiu Zhao felt that life was like this
从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks
Difference between JUnit theories and parameterized tests
石臻臻的2021总结和2022展望 | 文末彩蛋
RHCSA3
Database connection pool & jdbctemplate
Distributed solution - distributed session consistency problem