当前位置: 首页 > article >正文

蓝桥杯学习-11栈

11栈

先进后出

例题–蓝桥19877

用数组来设置栈

1.向栈顶插入元素--top位置标记元素
2.删除栈顶元素--top指针减减
3.输出栈顶元素--输出top位置元素

使用arraylist

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        ArrayList<Integer> array=new ArrayList<>();
        int m=sc.nextInt();
        for (int i = 0; i < m; i++){
            String str=sc.next();
            switch (str){
                case "push":
                    int x=sc.nextInt();
                    array.add(x);
                    break;
                case "pop":
                    if (array.size()==0){
                        break;
                    }else {
                        array.remove(array.size()-1);
                    }
                    break;
                case "empty":
                    if (array.size()==0){
                        System.out.println("YES");
                    }else {
                        System.out.println("NO");
                    }
                    break;
                case "query":
                    if (array.size()==0){
                        System.out.println("empty");
                    }else {
                        System.out.println(array.get(array.size()-1));
                    }
                    break;
                default:
                    System.out.println("error");
                    }
            }
        }
    }

使用数组

import java.io.*;
import java.util.*;
import java.math.BigInteger;

public class Main {
    static int N = (int)(1e5+10);
    static int[] stk = new int[N];
    static int top = 0;

    static void push(int x) {
        stk[++top] = x;
    }

    static void pop() {
        if(isEmpty()) return;
        top--;
    }

    static boolean isEmpty() {
        return top==0;
    }

    static void query() {
        if(isEmpty()) out.println("empty");
        else out.println(stk[top]);
    }

    static void solve() {
        int m = in.nextInt();
        while(m-->0) {
            String op = in.next();
            if(op.equals("push")) {
                int x = in.nextInt();
                push(x);
            }else if(op.equals("pop")) {
                pop();
            }else if(op.equals("empty")) {
                out.println(isEmpty()?"YES":"NO");
            }else {
                query();
            }
        }
    }

    public static void main(String[] args) {
        solve();
        out.flush();
    }

    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());
        }
    }
}

http://www.kler.cn/a/589506.html

相关文章:

  • Gone v2 Tracer 组件-给微服务提供统一的traceID
  • 深入解析Java面向对象三大特征之多态、final、抽象类与接口
  • CMake学习笔记(二):变量设值,源文件/文件查找
  • 网络安全运维应急响应与溯源分析实战案例
  • 【ES6】01-ECMAScript基本认识 + 变量常量 + 数据类型
  • Java高效构建树形结构——异步加载子节点的实现方案
  • “Failed to Load SteamUI.dll” 错误详解:全面解析与高效解决方案,助你快速修复 Steam 客户端问题
  • python拉取大视频导入deepseek大模型解决方案
  • ubuntu20.04下如何防止同一类型串口设备插入USB口无法区分到底是从/dev/ttyUSB0还是/dev/ttyUSB1读取数据
  • Java 8 Stream API:传统实现和流式编程的范式对比
  • 道格拉斯-普克算法
  • Android Room 框架公共模块源码深度剖析(四)
  • linux环境安装qnn_sdk 采坑记录
  • 事件驱动架构(EDA):微服务世界的未来趋势
  • LeetCode[206]反转链表
  • MySQL连接较慢原因分析及解决措施
  • C++基础 [五] - String的模拟实现
  • FlinkCDC 达梦数据库实时同步详解
  • java,poi,提取ppt文件中的文字内容
  • LLaMA-Factory微调sft Qwen2.5-VL-7B-Instruct