AcWing 828. 模拟栈
原题链接
简单
作者:
曲刹那只因芳华
,
2021-04-16 11:01:42
,
所有人可见
,
阅读 177
java 代码
import java.io.*;
import java.util.*;
class Main{
private final static int N = 100000+10;
private static int[] stack = new int[N];
private static int tt = -1;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int m = in.nextInt();
while(m-- >0){
String operate = in.next();
if("empty".equals(operate)){
System.out.println(empty()+"");
}else if("pop".equals(operate)){
pop();
}else if("push".equals(operate)){
int x = in.nextInt();
push(x);
}else{
System.out.println(query()+"");
}
}
}
private static void push(int x){
stack[++tt] = x;
}
private static int pop(){
return stack[tt--];
}
private static String empty(){
return tt==-1 ? "YES" : "NO";
}
private static int query(){
return stack[tt];
}
}