当前位置:网站首页>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 <= 1000
0 <= pushed[i] <= 1000
pushed
All elements of Different from each otherpopped.length == pushed.length
popped
yespushed
An 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();
}
}
边栏推荐
- JSON parsing error special character processing (really speechless... Troubleshooting for a long time)
- Notes for preparation of information system project manager --- information knowledge
- Free testing of Taobao tmall API order and flag insertion remark interface
- 奔跑,开路
- 使用 jMeter 对 SAP Spartacus 进行并发性能测试
- 单独编译内核模块
- NFT: how to make money with unique assets?
- MySQL 巨坑:update 更新慎用影响行数做判断!!!
- 2021-12-22 transaction record
- Transactions from January 14 to 19, 2022
猜你喜欢
RHCSA3
开发者,云原生数据库是未来吗?
How to connect the API interface of Taobao open platform (super detailed)
Why is your next computer a computer? Explore different remote operations
Shi Zhenzhen's 2021 summary and 2022 outlook | colorful eggs at the end of the article
初识Linkerd项目
Lepton 无损压缩原理及性能分析
Annotation problem and hidden Markov model
JSON parsing error special character processing (really speechless... Troubleshooting for a long time)
关于 SAP UI5 getSAPLogonLanguage is not a function 的错误消息以及 API 版本的讨论
随机推荐
在家庭智能照明中应用的测距传感芯片4530A
What if wechat is mistakenly sealed? Explain the underlying logic of wechat seal in detail
SAP SEGW 事物码里的 ABAP 类型和 EDM 类型映射的一个具体例子
Taobao product details API | get baby SKU, main map, evaluation and other API interfaces
946. 验证栈序列
I'm doing open source in Didi
Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution
stm32和电机开发(从架构图到文档编写)
Transactions from January 14 to 19, 2022
Taobao short video, why the worse the effect
A possible investment strategy and a possible fuzzy fast stock valuation method
Flume common commands and basic operations
Taobao order interface | order flag remarks, may be the most stable and easy-to-use interface
GPON technical standard analysis I
#yyds干货盘点#js截取文件后缀名
Introduction to the principle of DNS
View and terminate the executing thread in MySQL
SAP UI5 FlexibleColumnLayout 控件介绍
Laravel文档阅读笔记-mews/captcha的使用(验证码功能)
What is the difference between Bi software in the domestic market