当前位置:网站首页>Sword finger offer: push in and pop-up sequence of stack
Sword finger offer: push in and pop-up sequence of stack
2022-04-23 04:47:00 【wyplj_ sir】
subject
Enter two sequences of integers , The first sequence represents the push order of the stack , Determine whether the second sequence is the pop-up order for the stack . Suppose that all the Numbers pushed are not equal . for example , Sequence {1,2,3,4,5} Is the stack pressing sequence of a stack , Sequence {4,5,3,2,1} Is a popup sequence corresponding to the stack sequence , but {4,3,5,1,2} It cannot be a popup sequence of the push sequence .
Example
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
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 .
Code
Borrow an auxiliary stack stackstack , simulation Push the / Arrangement of pop-up operations . Depending on whether the simulation is successful , You get the result .
Into the stack, : Execute in the order of stack pressing sequence .
The stack, : After each stack , Circular judgement “ Top element of stack == Pop up the current element of the sequence ” Is it true , Pop up all stack top elements that meet the pop-up sequence order .
Answer key
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
if(pushed == null && popped == null){
return true;
}
if(pushed == null || popped == null){
return false;
}
if(pushed.length == 0 && popped.length == 0){
return true;
}
if(pushed.length != popped.length){
return false;
}
LinkedList<Integer> stack = new LinkedList<>();
int index = 0;
for(int i=0;i<pushed.length;i++){
stack.push(pushed[i]);
while(index < popped.length && !stack.isEmpty() && popped[index] == stack.peek()){
stack.pop();
index++;
}
}
return stack.isEmpty();
}
}
版权声明
本文为[wyplj_ sir]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220555179095.html
边栏推荐
- PHP+MySQL 制作留言板
- Code007 -- determine whether the string in parentheses matches
- Spark optimization
- List remove an element
- Agile practice | agile indicators to improve group predictability
- No such file or directory problem while executing shell
- Programmers complain: I really can't live with a salary of 12000. Netizen: how can I say 3000
- Simply drag objects to the item bar
- Innovative practice of short video content understanding and generation technology in meituan
- Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
猜你喜欢
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
redis数据类型有哪些
View analysis of scenic spots in ArcGIS
New terminal play method: script guidance independent of technology stack
C language: Advanced pointer
229. Find mode II
Innovation training (IX) integration
Spark small case - RDD, spark SQL
Spark FAQ sorting - must see before interview
随机推荐
383. Ransom letter
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
C language: spoof games
Leetcode003 -- judge whether an integer is a palindrome number
Logger and zap log Library in go language
CLion+OpenCV identify ID number - detect ID number
Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU
The last day of 2021 is the year of harvest.
拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!
Unity攝像頭跟隨鼠標旋轉
Learning Android from scratch -- Introduction
Spark FAQ sorting - must see before interview
leetcode002--将有符号整数的数字部分反转
Create VPC in AWS console (no plate)
Leetcode006 -- find the longest common prefix in the string array
View analysis of scenic spots in ArcGIS
IDE idea automatic compilation and configuration of on update action and on frame deactivation
Com alibaba. Common methods of fastjson
Innovation training (IX) integration
FAQ of foreign lead and alliance Manager