当前位置:网站首页>Determine whether a sequence is a stack pop-up sequence
Determine whether a sequence is a stack pop-up sequence
2022-06-26 18:11:00 【A goblin】
One , Title Description
Enter two sequences of integers , The first sequence represents the push order of the stack , Please determine if the second sequence is likely to be the pop-up sequence of the stack . Suppose that all the Numbers pushed are not equal . For example, the sequence 1,2,3,4,5 Is the push order 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 .( Be careful : The lengths of the two sequences are equal )
Two , Topic analysis
The two sequences given in question , One is the stack sequence , One is the stack sequence .
In the case of ensuring the order of the stack sequence , The alternate order of entering and leaving the stack , A sequence that causes the stack sequence to have a different order .
Their thinking :
- 1. If one of the popup sequence is empty , Go straight back to false
- 2. Use an auxiliary stack to save the stack sequence , A tag variable to locate the element position of the pop-up sequence
- 3. Loop stack , In the outer circle ( Traverse the stack sequence ), Recycling comparison (while), If s The stack is not empty and the top element of the stack is equal to the pop-up sequence popIndex position , Keep coming out of the stack
- 4, Finally, if the auxiliary stack is empty , It means that it is a return of the stack out method of the stack sequence true, If it is not empty, it is not , Go straight back to false
3、 ... and , Code up ( Read notes !!!)
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA.length == 0 || popA.length == 0){
// If one of the popup sequence is empty
return false;
}
Stack<Integer> s = new Stack<>(); // Used to organize elements that pop into the sequence
int popIndex = 0; // The position of the element in the sequence used to mark the position of the pop-up
for(int i = 0; i < pushA.length;i++){
// Loop through the popup sequence , Put on the stack in turn s
s.push(pushA[i]);
while(!s.isEmpty() && s.peek() == popA[popIndex]){
// If s The stack is not empty and the top element of the stack is equal to the pop-up sequence popIndex position
// Here the loop is the core , Determine whether it is one of many stack order
// Out of the stack
s.pop();
//pop Bit backward
popIndex++;
}
}
// The loop ends , If the stack s It's empty , It means that it is a pop-up sequence of the stack sequence
return s.isEmpty();
}
}边栏推荐
- Map and list < map > transfer to corresponding objects
- 腾讯钱智明:信息流业务中的预训练方法探索与应用实践
- next(iter(dataloader))的一点点体会
- VCD-影音光碟
- 深入理解MySQL锁与事务隔离级别
- Using redis for user access data statistics hyperloglog and bitmap advanced data types
- 二分查找-2
- Clion编译catkin_ws(ROS工作空间包的简称)加载CMakeLists.txt出现的问题
- 输入n个整数,输出出现次数大于等于数组长度一半的数
- DoS及攻擊方法詳解
猜你喜欢

Number of solutions for knapsack problem

How pycharm modifies multiline annotation shortcuts

数字签名标准(DSS)

Detailed explanation of dos and attack methods

In and exceptions, count (*) query optimization

Clion编译catkin_ws(ROS工作空间包的简称)加载CMakeLists.txt出现的问题

JVM entry Door (1)

必须要掌握的面试重点——索引和事务(附讲B-树与B+树)

LeetCode 128最长连续序列

in和exsits、count(*)查询优化
随机推荐
JS 常用正则表达式
How to open a stock account? Is it safe to open an account online now?
Preparing for the Blue Bridge Cup and ccf-csp
Properties file garbled
ZCMU--1367: Data Structure
Chinese (Simplified) language pack
The king of Internet of things protocol: mqtt
贝叶斯网络详解
Several key points in divorce agreement
Map and filter methods for processing scarce arrays
VCD video disc
Tencent qianzhiming: Exploration and application of pre training methods in information flow business
正则匹配相同字符
RSA encryption and decryption details
sql中的几种删除操作
Binary search-2
你好,现在网上股票开户买股票安全吗?
ros::spinOnce()和ros::spin()的使用和区别
Detailed explanation of dos and attack methods
Vscode usage - Remote SSH configuration description