当前位置:网站首页>判断某个序列是否为栈的弹出序列
判断某个序列是否为栈的弹出序列
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();
}
}边栏推荐
- 牛客网:设计LRU缓存结构 设计LFU缓存结构
- [ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers
- 深入理解MySQL锁与事务隔离级别
- Applet setting button sharing function
- MySQL index
- Analysis of deep security definition and encryption technology
- 14《MySQL 教程》INSERT 插入数据
- VSCode使用 - Remote-SSH 配置说明
- 国信证券怎么开户?通过链接办理股票开户安全吗
- 并发之线程安全
猜你喜欢

pycharm如何修改多行注释快捷键

Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!
![[buuctf.reverse] 126-130](/img/df/e35633d85caeff1dece62a66cb7804.png)
[buuctf.reverse] 126-130

Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC

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

transforms. The input of randomcrop() can only be PIL image, not tensor

分页查询、JOIN关联查询优化

DoS及攻击方法详解

让torch.cuda.is_available()从false变成true的一点经验

解决pycharm里面每个字母占一格空格的问题
随机推荐
[code Capriccio - dynamic planning] t583. Deleting two strings
Lm06 the mystery of constructing the bottom and top trading strategy only by trading volume
RSA加密解密详解
I want to know. I am in Zhaoqing. Where can I open an account? Is it safe to open an account online?
深层次安全定义剖析及加密技术
14 MySQL tutorial insert insert data
How does Guosen Securities open an account? Is it safe to open a stock account through the link
Let torch cuda. is_ Experience of available() changing from false to true
Concept and working principle of data encryption standard (DES)
Halcon's region: features of multiple regions (5)
halcon之区域:多种区域(Region)特征(5)
js强制转换
临时关闭MySQL缓存
一起备战蓝桥杯与CCF-CSP之大模拟炉石传说
#25class的类继承
清华&商汤&上海AI&CUHK提出Siamese Image Modeling,兼具linear probing和密集预测性能!
丰富专业化产品线, 江铃福特领睿·极境版上市
二分查找-2
JS cast
Discussion and generation of digital signature and analysis of its advantages