当前位置:网站首页>判断某个序列是否为栈的弹出序列
判断某个序列是否为栈的弹出序列
2022-06-26 18:01:00 【一只懐坏旭】
一,题目描述
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
二,题目分析
题中给出的两个序列,一个是入栈序列,一个是出栈序列。
在保证入栈序列的有序性的情况下,入栈出栈的交替顺序,导致出栈序列有不同的顺序的序列。
解题思路:
- 1.如果弹入或者弹出序列有一个为空,直接返回false
- 2.用一个辅助栈来保存入栈序列,一个标记变量来定位弹出序列的元素位置
- 3.循环入栈,外层循环中(遍历入栈序列),再循环对比(while),如果s栈不为空且此时栈顶元素等于弹出序列popIndex位,就持续出栈
- 4,最终如果辅助栈为空,就说明是该入栈序列的出栈方式中的一种返回true,不为空则不是,直接返回false
三,上代码(看注释!!!)
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){
//如果弹入或者弹出序列有一个为空
return false;
}
Stack<Integer> s = new Stack<>(); //用来组织弹入序列中的元素
int popIndex = 0; //用来标记该弹出的位置的元素在序列中的位置
for(int i = 0; i < pushA.length;i++){
//循环遍历弹入序列,依次放入栈s
s.push(pushA[i]);
while(!s.isEmpty() && s.peek() == popA[popIndex]){
//如果s栈不为空且此时栈顶元素等于弹出序列popIndex位
//此处循环是核心,判断是否为很多出栈顺序中的一个
//出栈
s.pop();
//pop位后移
popIndex++;
}
}
//循环结束,如果栈s为空,就说明是该入栈序列的的一个弹出序列
return s.isEmpty();
}
}边栏推荐
- 【uniapp】uniapp手机端使用uni.navigateBack失效问题解决
- MySQL add column failed because there was data before, not null by default
- [npoi] C copy sheet template across workbooks to export Excel
- 并发之Synchronized说明
- idea中文插件chinese(simplified) language pack
- Applet setting button sharing function
- Tencent qianzhiming: Exploration and application of pre training methods in information flow business
- Use FST JSON to automatically generate faster JSON serialization methods
- Discussion and generation of digital signature and analysis of its advantages
- 二分查找法-1
猜你喜欢

RSA加密解密详解

Treasure and niche CTA animation material website sharing

决策树与随机森林

No manual prior is required! HKU & Tongji & lunarai & Kuangshi proposed self supervised visual representation learning based on semantic grouping, which significantly improved the tasks of target dete

Niuke network: Design LRU cache structure design LFU cache structure

Data Encryption Standard DES security

MySQL exports all table indexes in the database

Detailed explanation of dos and attack methods
![[uniapp] the uniapp mobile terminal uses uni Troubleshooting of navigateback failure](/img/26/da00e70d0955bcdef3362474bc5fc7.png)
[uniapp] the uniapp mobile terminal uses uni Troubleshooting of navigateback failure

行锁分析和死锁
随机推荐
Prometeus 2.34.0 new features
Connected to surface test questions
[dynamic planning] Jianzhi offer II 091 Paint the house
RSA encryption and decryption details
Tsinghua & Shangtang & Shanghai AI & CUHK proposed Siamese image modeling, which has both linear probing and intensive prediction performance!
#25class的类继承
Synchronized description of concurrency
你好,现在网上股票开户买股票安全吗?
行锁分析和死锁
临时关闭MySQL缓存
pycharm如何修改多行注释快捷键
pycharm的plt.show()如何保持不关闭
有依赖的背包问题
Let torch cuda. is_ Experience of available() changing from false to true
next(iter(dataloader))的一点点体会
I want to know. I am in Zhaoqing. Where can I open an account? Is it safe to open an account online?
How to open a stock account? Is it safe to open an account online now?
Static registration and dynamic registration of JNI
牛客网:设计LRU缓存结构 设计LFU缓存结构
请指教同花顺开户选选择哪家券商比较好?现在在线开户安全么?