day10-232.用栈实现队列|225. 用队列实现栈

232.用栈实现队列

题目链接:https://leetcode.cn/problems/implement-queue-using-stacks/submissions/

题目要求:

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):

实现 MyQueue 类:

    void push(int x) 将元素 x 推到队列的末尾
    int pop() 从队列的开头移除并返回元素
    int peek() 返回队列开头的元素
    boolean empty() 如果队列为空,返回 true ;否则,返回 false

🤔解题思路

进队列的时候直接push入栈就可以,出队列的时候比较复杂,由于队列是先进先出原则,需要先判断出栈是否为空,如果不为空,则直接出栈pop,如果为空,则必须先把入栈全部导入出栈 (用于颠倒元素顺序,模拟队列出栈顺序)再出栈;

class MyQueue {
    public Stack<Integer> stackIn;    //模拟进队列
    public Stack<Integer> stackOut;    //模拟出队列
    public MyQueue() {
        stackIn = new Stack();
        stackOut = new Stack();
    }
    
    public void push(int x) {
        stackIn.push(x);
    }
    
    public int pop() {
        reverse();
        return stackOut.pop();
    }
    
    public int peek() {
        int i = pop();
        stackOut.push(i);
        return i;
    }
    
    //如果进栈出栈都为空,则说明队列为空
    public boolean empty() {
        return stackIn.isEmpty() && stackOut.isEmpty();
    }

    // 如果stackOut为空,那么将stackIn中的元素全部放到stackOut中
    public void reverse(){
        if(stackOut.isEmpty()){
            while(!stackIn.isEmpty()){
                stackOut.push(stackIn.pop());
            }
        }else{
            return;
        }
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */

225. 用队列实现栈

题目链接:https://leetcode.cn/problems/implement-stack-using-queues/submissions/

题目要求:

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。

实现 MyStack 类:

    void push(int x) 将元素 x 压入栈顶。
    int pop() 移除并返回栈顶元素。
    int top() 返回栈顶元素。
    boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

🤔解题思路

入栈正常add进队列;出栈的时候要先将size-1个元素出队列,再重新入队列,使得最后进队列的元素位于队列最前端,再进行出栈操作

class MyStack {
    Queue<Integer> queue;
    int size;
    public MyStack() {
        queue = new LinkedList();
        size = 0;
    }
    
    public void push(int x) {
        queue.add(x);
        size++;
    }
    
    public int pop() {
        for(int i = size;i > 1;i--){
            queue.add(queue.poll());
        }
        if(!queue.isEmpty()) size--;
        return queue.poll();
    }
    
    public int top() {
        int i = pop();
        size++;
        queue.add(i);
        return i;
    }
    
    public boolean empty() {
        return queue.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值