手撕算法-队列实现栈And栈实现队列

手撕算法-队列实现栈And栈实现队列


两个栈实现队列

image.png
分析:
转换数据方向,第一个栈写,第二个栈读。

代码:

import java.util.*;
import java.util.Stack;

public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    
    public void push(int node) {
        stack1.push(node);
    }
    
    public int pop() {
        if(stack2.isEmpty()) {
            while(!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }

        return stack2.pop();
    }
}

两个队列实现栈

image.png
分析:
先进先出,变先进后出。两个队列,

  • push时,push到q1
  • pop时,先将q1的元素转移到q2中,最终q1只剩一个元素,长度为1,此时即为最后push进来的,后进先出
  • pop完成后,需要交换q1和q2,方便下次pop
  • push操作始终在q1

代码:

public class MyStack {
    // 进行push操作
    Queue<Integer> q1 = new LinkedList<>();
    // 进行pop操作时,存储之前的值
    Queue<Integer> q2 = new LinkedList<>();

    public void push(int node) {
        q1.add(node);
    }

    public int pop() {
        while (q1.size() > 1) {
            q2.add(q1.poll());
        }

        // 交换q1和q2
        Queue<Integer> tmp = q1;
        q1 = q2;
        q2 = tmp;

        return q2.poll();
    }

    public int top(){
        while (q1.size() > 1) {
            q2.add(q1.poll());
        }
        int res = q1.peek();

        // 最后一个也放进去q2,然后交换q1和q2
        q2.add(q1.poll());

        // 交换q1和q2
        Queue<Integer> tmp = q1;
        q1 = q2;
        q2 = tmp;

        return res;
    }

    public boolean empty() {
        return q1.isEmpty() && q2.isEmpty();
    }

    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);


        System.out.println(myStack.top());
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.empty());
    }
}

image.png
输出结果符合预期

包含min函数的栈

image.png
分析:
image.png
代码:

import java.util.*;
import java.util.Stack;

public class Solution {
    //用于栈的push 与 pop
    Stack<Integer> s1 = new Stack<Integer>();
    //用于存储最小min
    Stack<Integer> s2 = new Stack<Integer>();

    public void push(int node) {
        s1.push(node);
        // 空或者新元素较小,则入栈
        if (s2.isEmpty() || s2.peek() > node) {
            s2.push(node);
        } else {
            // 最小的还是原来s2的栈顶,再次入栈,表示这么多数据的最小值
            s2.push(s2.peek());
        }
    }

    public void pop() {
        s1.pop();
        s2.pop();
    }

    public int top() {
        return s1.peek();
    }

    public int min() {
        return s2.peek();
    }
}

相关推荐

  1. 225.队列实现

    2024-03-21 05:36:03       11 阅读
  2. 实现队列

    2024-03-21 05:36:03       13 阅读
  3. 面试算法-119-用实现队列

    2024-03-21 05:36:03       14 阅读
  4. 面试算法-118-用队列实现

    2024-03-21 05:36:03       20 阅读
  5. c++ 实现、单向队列、双向队列

    2024-03-21 05:36:03       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-21 05:36:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-21 05:36:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-21 05:36:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-21 05:36:03       20 阅读

热门阅读

  1. L2-048 寻宝图 (DFS做法)

    2024-03-21 05:36:03       22 阅读
  2. LinkedList源码解析和设计思路

    2024-03-21 05:36:03       22 阅读
  3. android 音频焦点,音频策略梳理

    2024-03-21 05:36:03       21 阅读
  4. 《PHP 魔法之旅:探索基础知识与技巧》

    2024-03-21 05:36:03       20 阅读
  5. Mac中用python安装lightgbm报错image not found

    2024-03-21 05:36:03       19 阅读
  6. Selenium 学习(0.21)——软件测试之单元测试

    2024-03-21 05:36:03       23 阅读
  7. Stable Diffusion训练图片时,简陋的数据处理

    2024-03-21 05:36:03       14 阅读