栈与队列知识点总结
知识点
用数组模拟栈
tt表示的是栈顶的下标 stk[tt]代表的栈
用数组模拟队列
## 栈适用的题型
找到左边或右边 离这个数最近的数 且比它小的数或大的数
while (tt && stk[tt] >= x) tt -- ;//如果栈顶元素大于当前待入栈元素,则出栈
if (!tt) printf("-1 ");//如果栈空,则没有比该元素小的值。
else printf("%d ", stk[tt]);//栈顶元素就是左侧第一个比它小的元素。
stk[ ++ tt] = x;
acwing 828. 模拟栈
代码案例:输入样例:
10
push 5
query
push 6
pop
query
pop
empty
push 4
query
empty
输出样例:
5
5
YES
4
NO
题解
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int m = scan.nextInt();
int[] stl = new int[100010];
int tt = 0;
while(m -- > 0){
String s = scan.next();
if(s.equals("push")){
int x= scan.nextInt();
stl[++tt] = x;
}else if(s.equals("pop")){
tt--;
}else if(s.equals("empty")){
if(tt > 0){
System.out.println("NO");
}else System.out.println("YES");
}else{
System.out.println(stl[tt]);
}
}
}
}
单调栈
给定一个长度为 N 的整数数列,输出每个数左边第一个比它小的数,如果不存在则输出 −1
输入格式
第一行包含整数 N,表示数列长度。
第二行包含 N个整数,表示整数数列。
输出格式共一行,包含 N个整数,其中第 i 个数表示第 i 个数的左边第一个比它小的数,如果不存在则输出 −1。
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] stk = new int[100010];
int tt = 0;
for(int i = 0 ; i < n ; i++ ){
int x = scan.nextInt();
//如果栈是空的,栈顶元素大于等于x,那么就说明栈顶这个数明显没有x好,
//因为比较的是左边第一个最近最小的数,所以就把stk[tt]弹出了;
while(tt!=0 && stk[tt] >= x){//如果栈顶元素大于当前待入栈元素,则出栈
tt--;
}
//如果弹出操作完了之后,栈不是空的,就输出栈顶元素,
if(tt != 0) System.out.print(stk[tt]+" ");
//否则就是栈是空的,输出-1
else System.out.print("-1" + " ");
//最后将x插入栈顶;
stk[++tt] = x;
}
}
}
import java.util.*;
import java.io.*;
public class Main {
static int N = 100010;
static int[] a = new int[N];
static int[] s = new int[N];
static int tt = 0 ;
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
String[] arr = in.readLine().split(" ") ;
for(int i = 0 ; i< n ; i++){
a[i]= Integer.parseInt(arr[i]);
}
for(int i = 0 ; i < n ; i++){
while(tt != 0 && s[tt] >= a[i]) tt--;//如果栈顶元素大于当前待入栈元素,则出栈
if(tt == 0) System.out.print("-1"+" ");//如果栈空,则没有比该元素小的值。
else System.out.print(s[tt]+" ");//栈顶元素就是左侧第一个比它小的元素。
s[ ++ tt] = a[i];//加入栈
}
}
}
模拟队列模板题
输入样例:
10
push 6
empty
query
pop
empty
push 3
push 4
pop
query
push 6
输出样例:
NO
6
YES
4
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int m = scan.nextInt();
//队列是在tt队尾插入元素,队头hh弹出元素
int[] dl = new int[100010];
int hh = 0;
int tt = -1;
while(m -- > 0){
String s = scan.next();
if(s.equals("push")){
int x = scan.nextInt();
dl[++tt] = x;
}else if(s.equals("pop")){
hh++;
}else if(s.equals("empty")){
if(hh <= tt) System.out.println("NO");
else System.out.println("YES");
}else {
System.out.println(dl[hh]);
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)