当前位置:网站首页>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();
}
}边栏推荐
- RuntimeError: CUDA error: out of memory自己的解决方法(情况比较特殊估计对大部分人不适用)
- I want to know. I am in Zhaoqing. Where can I open an account? Is it safe to open an account online?
- pycharm如何修改多行注释快捷键
- 数字签名标准(DSS)
- RSA概念详解及工具推荐大全 - lmn
- 正则匹配相同字符
- 判断某个序列是否为栈的弹出序列
- RSA concept explanation and tool recommendation - LMN
- 腾讯钱智明:信息流业务中的预训练方法探索与应用实践
- 带你解决哈希冲突,并实现一个简单hash表,
猜你喜欢
随机推荐
深入理解MySQL锁与事务隔离级别
9、智慧交通项目(2)
Tencent qianzhiming: Exploration and application of pre training methods in information flow business
in和exsits、count(*)查询优化
手写promise.all
我想知道,我在肇庆,到哪里开户比较好?网上开户是否安全么?
Bayesian network explanation
JVM entry door (1)
非对称密码体制详解
How does Guosen Securities open an account? Is it safe to open a stock account through the link
图像二值化处理
RuntimeError: CUDA error: out of memory自己的解决方法(情况比较特殊估计对大部分人不适用)
DoS及攻擊方法詳解
Runtimeerror: CUDA error: out of memory own solution (it is estimated that it is not applicable to most people in special circumstances)
深层次安全定义剖析及加密技术
Detailed explanation of dos and attack methods
KDD 2022 | how to use comparative learning in cross domain recommendation?
比较两个对象的大小关系原来可以如此花里胡哨
Concept and working principle of data encryption standard (DES)
数据加密标准(DES)概念及工作原理









