当前位置:网站首页>『牛客|每日一题』 栈的压入、弹出序列
『牛客|每日一题』 栈的压入、弹出序列
2022-07-26 06:34:00 【starry陆离】
作者简介:一位喜欢写作,计科专业大二菜鸟
个人主页:starry陆离
首发日期:2022年7月14日星期四
上期文章:『牛客|每日一题』模板栈
订阅专栏:『牛客刷题集锦』
如果文章有帮到你的话记得点赞+收藏支持一下哦

1.每日一题

2.方法一:辅助栈
2.1思路分析
题目要我们判断两个序列是否符合入栈出栈的次序,我们就可以用一个栈来模拟。对于入栈序列,只要栈为空,序列肯定要依次入栈。那什么时候出来呢?自然是遇到一个元素等于当前的出栈序列的元素,那我们就放弃入栈,让它先出来。
//入栈:栈为空或者栈顶元素不等于出栈数组(注意还要控制数组越界j<n)
while(j<n&&(stack.isEmpty()||stack.peek()!=popA[i])){
stack.push(pushA[j++]);
}
2.2具体做法
- step 1:准备一个辅助栈,两个下标分别访问两个序列。
- step 2:辅助栈为空或者栈顶不等于出栈数组当前元素,就持续将入栈数组加入栈中。
- step 3:栈顶等于出栈数组当前元素就出栈。
- step 4:当入栈数组访问完,出栈数组无法依次弹出,就是不匹配的,否则两个序列都访问完就是匹配的。
2.3代码题解
import java.util.*;
import java.util.ArrayList;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
//元素的个数
int n=pushA.length;
//辅助栈
Stack<Integer>stack=new Stack<Integer>();
//遍历入栈的下标
int j=0;
//遍历出栈数组,i为出栈数组下标
for(int i=0;i<n;++i){
//入栈:栈为空或者栈顶元素不等于出栈数组(注意还要控制数组越界j<n)
while(j<n&&(stack.isEmpty()||stack.peek()!=popA[i])){
stack.push(pushA[j++]);
}
//栈顶元素等于出栈数组中元素
if(stack.peek()==popA[i]){
stack.pop();
}else{
//否则为不匹配序列
return false;
}
}
return true;
}
}

3.方法二:原地栈
3.1思路分析
方法一我们使用了一个辅助栈来模拟,但是数组本来就很类似栈啊,用下标表示栈顶。在方法一中push数组前半部分入栈了,就没用了,这部分空间我们就可以用来当成栈。原理还是同方法一一样,只是这时我们遍历push数组的时候,用下标n表示栈空间,n的位置就是栈顶元素。
3.2具体做法
- step 1:用下标i表示栈空间,用j表示出栈序列的下标。
- step 2:遍历每一个待入栈的元素,加入栈顶,即push数组中i的位置,同时增加栈空间的大小,即i的大小。
- step 3:当栈不为空即栈顶i大于等于0,且栈顶等于当前出栈序列,就出栈,同时缩小栈的空间,即减小i。
- step 4:最后若是栈空间大小i为0,代表全部出栈完成,否则不匹配。
3.3代码题解
import java.util.*;
import java.util.ArrayList;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
//栈空间的大小
int i=0;
//出栈序列下标
int j=0;
//遍历入栈数组
for(int a:pushA){
//将元素加入栈顶
pushA[i]=a;
//当栈不空且栈顶元素等于出栈数组元素时
while(i>=0&&pushA[i]==popA[j]){
j++;
//元素出栈,缩小栈空间大小
i--;
}
//为下一个元素入栈做准备
i++;
}
//栈空间的大小为0说明元素全部出栈
return i==0;
}
}

边栏推荐
- [day_020419] sort subsequence
- [fault diagnosis] bearing fault diagnosis based on Bayesian optimization support vector machine with matlab code
- 快速排序(quick-sort)
- English sentence pattern reference exclusive Edition - attributive clause
- C language introduction practice (8): switch case calculates the month, year and day of the next day (normal year / leap year calculation)
- Interpretation of TPS motion (cvpr2022) video generation paper
- Force buckle - 4. Find the median of two positive arrays
- 【Day_04 0421】计算糖果
- Nuxt configuration topic switching
- BPG notes (IV)
猜你喜欢

Yolov6: the fast and accurate target detection framework is open source

PG Vacuum 杂谈之 auto vacuum

Do you think you are a reliable test / development programmer? "Back to the pot"? Surface and reality
![[image denoising] image denoising based on bicube interpolation and sparse representation matlab source code](/img/39/716c62d6ca533a7e84704b2c55d072.png)
[image denoising] image denoising based on bicube interpolation and sparse representation matlab source code

JVM class loading and GC garbage collection mechanism

【故障诊断】基于贝叶斯优化支持向量机的轴承故障诊断附matlab代码

【无标题】

白盒测试的概念、目的是什么?及主要方法有哪些?

SQL optimization scheme

【pytorch】图片增广
随机推荐
【Day_02 0419】排序子序列
RNN recurrent neural network
[day_040421] binary conversion
数据库中varchar和Nvarchar区别与联系
BPG notes (IV)
Liberg avenue to Jane series
YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code
[day06_0423] C language multiple choice questions
【Day_01 0418】删除公共字符串
排序问题:冒泡排序,选择排序,插入排序
Show you the principle of IO multiplexing (select, poll and epoll)
[day_030420] find the longest consecutive number string in the string
【Day_05 0422】连续最大和
【Day_04 0421】计算糖果
归并排序(merge_sort)
[pytorch] CNN practice - flower species identification
Database and the future of open source
[untitled]
Find the original root
