Java刷题-stack
阅读原文时间:2023年07月09日阅读:1

实现一个特殊功能的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作。

第一行输入一个整数N,表示对栈进行的操作总数。

下面N行每行输入一个字符串S,表示操作的种类。

如果S为"push",则后面还有一个整数X表示向栈里压入整数X。

如果S为"pop",则表示弹出栈顶操作。

如果S为"getMin",则表示询问当前栈中的最小元素是多少。

对于每个getMin操作,输出一行表示当前栈中的最小元素是多少。

import java.util.Scanner;
import java.util.Stack;

public class Main {
    private static Stack<Integer> stackData = new Stack<>();
    private static Stack<Integer> stackMin = new Stack<>();

    public static void push(int newNum) {
        stackData.push(newNum);
        if (stackMin.isEmpty()) {
            stackMin.push(newNum);
        } else if (newNum <= stackMin.peek()) {
            stackMin.push(newNum);
        }
    }

    public static int pop() {
        int value = stackData.pop();
        if (value == stackMin.peek()) {
            stackMin.pop();
        }
        return value;
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();    //注意这个地方需要用nextLine()读取回车
        for (int i = 0; i < n; i++) {
            String str = sc.nextLine();
            if (str.equals("getMin")) {
                System.out.println(stackMin.peek());
            } else if (str.equals("pop")) {
                pop();
            } else {
                String[] a = str.split(" ");
                int num = Integer.parseInt(a[a.length - 1]);
                push(num);
            }

        }
    }
}

nextInt(): it only reads the int value, nextInt() places the cursor in the same line after reading the input.

next(): read the input only till the space. It can't read two words separated by space. Also, next() places the cursor in the same line after reading the input.

nextLine():  reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.

nextInt():读取int类型的值,取值后,并不换行,光标不变

如果读入的字符串需要读入空格的话,注意需要用nextline读入nextInt后面输入的空格,否则默认就将回车读入了

public class ceshi {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        int num=sc.nextInt();
        String num1=sc.nextLine();
        System.out.println(num);
        System.out.println(num1);
    }
}

3
3

nextLine()可以读取"\n"并换行输出,光标换行

next()只读空格之前的数据,光标换行

Java Stack 类

栈是Vector的一个子类,它实现了一个标准的后进先出的栈。

堆栈只定义了默认构造函数,用来创建一个空栈。 堆栈除了包括由Vector定义的所有方法,也定义了自己的一些方法。

Stack()

除了由Vector定义的所有方法,自己也定义了一些方法:

序号

方法描述

1

boolean empty() 
测试堆栈是否为空。

2

Object peek( )
查看堆栈顶部的对象,但不从堆栈中移除它。

3

Object pop( )
移除堆栈顶部的对象,并作为此函数的值返回该对象。

4

Object push(Object element)
把项压入堆栈顶部。

5

int search(Object element)
返回对象在堆栈中的位置,以 1 为基数。

字符串向整型的转换

字符串->整型
使用Integer类中的parseInt()方法

整型->字符串

  • 任何类型+""可变成String类型(更简洁)
  • 使用静态方法toString()

二、由两个栈组成的队列

编写一个类,用两个栈来实现队列,支持队列的基本操作

都是套路,学会就行,进步了哈哈

两个注意点

1、压入数据要全压

2、负责队列的栈不为空,就不能压入栈

import java.util.Scanner;
import java.util.Stack;

public class Main {
    private static Stack<Integer> stack1 = new Stack<>();
    private static Stack<Integer> stack2 = new Stack<>();

    public static void add(int num) {
        stack1.push(num);
    }

    public static int poll() {
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                int num = stack1.pop();
                stack2.push(num);
            }
        }
        return stack2.pop();
    }

    public static int peek() {
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                int num = stack1.pop();
                stack2.push(num);
            }
        }
        return stack2.peek();
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        for (int i = 0; i < n; i++) {
            String str = sc.nextLine();
            if (str.equals("peek")) {
                System.out.println(peek());
            } else if (str.equals("poll")) {
                poll();
            } else {
                String[] a = str.split(" ");
                int num = Integer.parseInt(a[a.length - 1]);
                add(num);
            }
        }
    }

}

三、递归逆序一个栈

这个题目对于递归理解有很好的帮助,我的理解就是盗梦空间,不停的进入下一个空间,直到到底层梦境中,死亡的时候就会逐层返回,这样的操作可以帮助我们转换一些数字顺序。

多看看吧,递归确实麻烦

import java.util.Scanner;
import java.util.Stack;

public class ss {

    public static int reverse_Top(Stack<Integer> stack) {
        int top = stack.pop();
        if (stack.isEmpty()) {
            return top;
        } else {
            int last = reverse_Top(stack);
            stack.push(top);
            return last;
        }
    }

    public static void reverse_stack(Stack<Integer> stack) {
        if (stack.isEmpty()) {
            return;
        }
        int last = reverse_Top(stack);
        reverse_stack(stack);
        stack.push(last);
    }

    public static void main(String[] args) {
        Stack<Integer> stack_new = new Stack<>();
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int[] arr = new int[num];
        for (int i = 0; i < num; i++) {
            arr[i] = sc.nextInt();
        }
        for (int i = num - 1; i >= 0; i--) {
            stack_new.push(arr[i]);
        }
        reverse_stack(stack_new);
        for (int i = 0; i < num; i++) {
            System.out.print(stack_new.pop() + " ");
        }
    }
}

四、猫狗队列

五、用一个栈实现另一个栈的排序

这个题目注意循环的使用,唉不要乱用判断

import java.util.Scanner;
import java.util.Stack;

public class Stack_05 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int num2 = 0;
        Stack<Integer> stack = new Stack<>();
        Stack<Integer> stack1 = new Stack<>();
        for (int i = 0; i < num; i++) {
            num2 = sc.nextInt();
            stack.push(num2);
        }

        while (!stack.isEmpty()) {
            int cur = stack.pop();
            while (!stack1.isEmpty() && stack1.peek() < cur) {
                stack.push(stack1.pop());
            }
            stack1.push(cur);
        }

        while (!stack1.isEmpty()) {
            stack.push(stack1.pop());
        }
        for (int i = 0; i < num; i++) {
            System.out.print(stack.pop()+" ");
        }
    }
}

六、生成窗口最大值数组

需要注意的是Linkedlist的用法一般用来形成队列,双端队列就可以实现这个题目的求解。

java.util.LinkedList 集合数据存储的结构是链表结构。方便元素添加、删除的集合。
LinkedList是一个双向链表,那么双向链表是什么样子的呢,我们用个图了解下。

实际开发中对一个集合元素的添加与删除经常涉及到首尾操作,而LinkedList提供了大量首尾操作的方法.

add poll peek三种

import java.util.LinkedList;
import java.util.Scanner;

public class sdfd {
    public static int[] getMaxWindows(int[] arr, int w) {
        //错误判断
        if (arr == null || w < 1 || arr.length < w) {
            return null;
        }
        LinkedList<Integer> qmax = new LinkedList<>();
        int[] res = new int[arr.length - w + 1];
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            while (!qmax.isEmpty() && arr[qmax.peekLast()] <= arr[i]) {
                qmax.pollLast();
            }
            qmax.addLast(i);
            if (qmax.peekFirst() == i - w) {
                qmax.pollFirst();
            }
            if(i>w-1){
                res[index++]=arr[qmax.peekFirst()];
            }
        }
        return res;

    }

    public static void main(String[] args) {

    }

}