1 条题解

  • 0
    @ 2025-5-8 22:18:48

    JAVA AC代码:

    
    import java.util.*;
    import java.util.StringTokenizer;
    import java.io.*;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Stack<Integer> stack=new Stack<>();//掌握util库调用Stack(栈)
    		int m=in.nextInt();
    		while(m-->0){
    			String op=in.next();
    			if(op.equals("push")){
    				int x=in.nextInt();
    				stack.push(x);
    			}else if(op.equals("pop")){
    				if(!stack.isEmpty()) {
    					stack.pop();
    				}
    			}else if(op.equals("empty")) {
    				if(stack.isEmpty()) out.println("YES");
    				else out.println("NO");
    			}else {
    				if(stack.isEmpty()) out.println("empty");
    				else out.println(stack.peek());
    			}
    		}
    		out.close();
    
    	}
    	
    	
    
    	static FastReader in=new FastReader();
    	static PrintWriter out=new PrintWriter(System.out);
    	static class FastReader{
    		static BufferedReader br;
    		static StringTokenizer st;
    		FastReader(){
    			br=new BufferedReader(new InputStreamReader(System.in));
    		}
    		String next() {
    			String str="";
    			while(st==null||!st.hasMoreElements()) {
    				try {
    					str=br.readLine();
    				}catch(IOException e) {
    					throw new RuntimeException(e);
    				}
    				st=new StringTokenizer(str);
    			}
    			return st.nextToken();
    		}
    		int nextInt() {
    			return Integer.parseInt(next());
    		}
    		double nextDouble() {
    			return Double.parseDouble(next());
    		}
    		long nextLong() {
    			return Long.parseLong(next());
    		}
    	}
    	
    }
    
    
    • 1

    信息

    ID
    59
    时间
    1000ms
    内存
    256MiB
    难度
    2
    标签
    递交数
    40
    已通过
    19
    上传者