当前位置:网站首页>946. Verify stack sequence
946. Verify stack sequence
2022-07-05 12:54:00 【accumulate steadily ض】
Given pushed and popped Two sequences , In each sequence The values are not repeated , Only if they could have been pushed on the original empty stack push And pop up pop When manipulating the result of a sequence , return true; otherwise , return false .
Example 1:
Input :pushed = [1,2,3,4,5], popped = [4,5,3,2,1] Output :true explain : We can do it in the following order : push(1), push(2), push(3), push(4), pop() -> 4, push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
Example 2:
Input :pushed = [1,2,3,4,5], popped = [4,3,5,1,2] Output :false explain :1 Can't be in 2 Pop up before .
Tips :
1 <= pushed.length <= 10000 <= pushed[i] <= 1000pushedAll elements of Different from each otherpopped.length == pushed.lengthpoppedyespushedAn arrangement of
Our approach is to stack the stack sequence first , When the out of stack sequence is the same as the in stack sequence , The elements at the top of the stack should be out of the stack , At the same time, the stack sequence then traverses the next element
This is the same situation :

This is a different situation :

Ideas :
Let the stack queue stack one by one , See whether the top element of the stack is the same as the out of stack queue element , If it's not the same , Let the stack queue stack one by one , If it is the same, let the top element out of the stack , At the same time, the stack queue traverses the next element , Continue reciprocating cycle , Finally, if the stack array traversal is completed and the stack is empty, it proves that the stack queue is the same as the stack queue , If the last stack array traversal is completed and the stack is not empty , That proves that the stack in queue is different from the stack out queue .
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]);// Push the stack sequence onto the stack
while(!s.isEmpty() && index<popped.length&&popped[index] == s.peek()){
// When the stack is not empty and index Do not cross the boundary to see whether the stack sequence is the same as the top element
s.pop();// If the same pop-up stack top element
index++;// Traverse the next element
}
}
return s.isEmpty();
}
}边栏推荐
- GPON other manufacturers' configuration process analysis
- 谈谈我写作生涯的画图技巧
- 10 minute fitness method reading notes (1/5)
- 155. 最小栈
- Why is your next computer a computer? Explore different remote operations
- Simply take stock reading notes (1/8)
- Transactions from December 29, 2021 to January 4, 2022
- RHCAS6
- 自然语言处理从小白到精通(四):用机器学习做中文邮件内容分类
- [Nacos cloud native] the first step of reading the source code is to start Nacos locally
猜你喜欢

SAP SEGW 事物码里的导航属性(Navigation Property) 和 EntitySet 使用方法

Annotation problem and hidden Markov model

SAP 自开发记录用户登录日志等信息

UNIX socket advanced learning diary - advanced i/o functions

Lepton 无损压缩原理及性能分析

滴滴开源DELTA:AI开发者可轻松训练自然语言模型

Distance measuring sensor chip 4530a used in home intelligent lighting

SAP UI5 DynamicPage 控件介绍

Transactions on December 23, 2021

【云原生】Nacos中的事件发布与订阅--观察者模式
随机推荐
How can labels/legends be added for all chart types in chart. js (chartjs.org)?
SAP UI5 ObjectPageLayout 控件使用方法分享
GPON technical standard analysis I
Yyds dry inventory JS intercept file suffix
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
Kotlin process control and circulation
Language model
Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
SAP UI5 DynamicPage 控件介绍
上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花
Kotlin函数
Kotlin流程控制、循环
OPPO小布推出预训练大模型OBERT,晋升KgCLUE榜首
Wechat enterprise payment to change access, open quickly
Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution
Flume common commands and basic operations
Transactions from December 29, 2021 to January 4, 2022
Kotlin变量
Pinduoduo flag insertion remarks API
非技术部门,如何参与 DevOps?