代码随想录第十天

232.用栈实现队列

题目链接/文章讲解/视频讲解:https://programmercarl.com/0232.%E7%94%A8%E6%A0%88%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97.html

思路:

实现这个先入先出队列可以使用两个栈来模拟。一个栈用于入队操作,另一个栈用于出队操作。

步骤:

  1. 定义两个栈 stack1stack2,其中 stack1 用于入队操作,stack2 用于出队操作。
  2. 入队操作 push:直接将元素 x 压入 stack1
  3. 出队操作 pop:如果 stack2 是空的,则将 stack1 中的所有元素依次弹出并压入 stack2,此时 stack2 的栈顶元素就是队列的开头元素,将其弹出即可。如果 stack2 不为空,则直接弹出 stack2 的栈顶元素。
  4. 返回队列开头元素 peek:如果 stack2 是空的,则将 stack1 中的所有元素依次弹出并压入 stack2,此时 stack2 的栈顶元素就是队列的开头元素,返回它。如果 stack2 不为空,则直接返回 stack2 的栈顶元素。
  5. 判断队列是否为空 empty:当 stack1stack2 都是空的时候,队列为空,返回 true,否则返回 false

完整代码

import java.util.Stack;

class MyQueue {
    private Stack<Integer> stack1;
    private Stack<Integer> stack2;

    /** Initialize your data structure here. */
    public MyQueue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        stack1.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
    
    /** Get the front element. */
    public int peek() {
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.peek();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }

    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.push(1);
        myQueue.push(2);
        int peek = myQueue.peek();
        System.out.println(peek); // 输出 1
        int pop = myQueue.pop();
        System.out.println(pop); // 输出 1
        boolean empty = myQueue.empty();
        System.out.println(empty); // 输出 false
    }
}

225. 用队列实现栈

题目链接/文章讲解/视频讲解:https://programmercarl.com/0225.%E7%94%A8%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E6%A0%88.html

两个队列

思路:

用两个队列 queue1queue2。入栈操作直接将元素加入到 queue1 中,并更新 topElement 为最新的元素。出栈操作需要将 queue1 中的元素依次移动到 queue2 中,直到只剩下一个元素,将其弹出并返回。然后交换 queue1queue2 的引用,使得 queue1 变为空队列,queue2 变为下一次出栈操作所需的队列。返回栈顶元素时直接返回 topElement。判断栈是否为空只需要判断 queue1 是否为空。

整体思路就是通过两个队列来模拟栈的后入先出(LIFO)特性,每次入栈时都更新 topElement 为最新入栈的元素,并将所有元素移动到另一个队列中以保证栈顶元素在队列的最前面,这样出栈操作就可以按照栈的后入先出的顺序进行了。

完整代码:
import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    private Queue<Integer> queue1;
    private Queue<Integer> queue2;
    private int topElement;

    /** Initialize your data structure here. */
    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        queue1.add(x);
        topElement = x;
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        while (queue1.size() > 1) {
            topElement = queue1.remove();
            queue2.add(topElement);
        }
        int poppedElement = queue1.remove();
        Queue<Integer> temp = queue1;
        queue1 = queue2;
        queue2 = temp;
        return poppedElement;
    }
    
    /** Get the top element. */
    public int top() {
        return topElement;
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue1.isEmpty();
    }

    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        int top = myStack.top();
        System.out.println(top); // 输出 2
        int pop = myStack.pop();
        System.out.println(pop); // 输出 2
        boolean empty = myStack.empty();
        System.out.println(empty); // 输出 false
    }
}

一个队列

思路:

用一个队列 queue 来模拟栈的操作。入栈操作时,先将元素加入队列中,然后对队列中的前 size 个元素依次出队再入队,这样就保证了刚入队的元素在队列的最前面,相当于栈顶元素。出栈操作直接将队列中的元素出队即可。返回栈顶元素时直接返回队列的首个元素。判断栈是否为空只需要判断队列是否为空。通过这样的方式,我们可以用一个队列来实现栈的操作。

步骤:
  1. 使用一个队列 queue 来存储栈中的元素。
  2. 入栈操作时,先获取当前队列的大小 size,然后将元素 x 入队到队列中。接着对队列中的前 size 个元素依次出队再入队,这样就保证了刚入队的元素在队列的最前面,相当于栈顶元素。
  3. 出栈操作时,直接将队列中的元素出队即可。
  4. 获取栈顶元素时,返回队列的首个元素。
  5. 判断栈是否为空时,判断队列是否为空。
完整代码:
import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    private Queue<Integer> queue;

    /** Initialize your data structure here. */
    public MyStack() {
        queue = new LinkedList<>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        int size = queue.size();
        queue.offer(x);
        for (int i = 0; i < size; i++) {
            queue.offer(queue.poll());
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue.poll();
    }
    
    /** Get the top element. */
    public int top() {
        return queue.peek();
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue.isEmpty();
    }

    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        int top = myStack.top();
        System.out.println(top); // 输出 2
        int pop = myStack.pop();
        System.out.println(pop); // 输出 2
        boolean empty = myStack.empty();
        System.out.println(empty); // 输出 false
    }
}

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值