【Leetcode】Implement Queue using Stacks

本文详细介绍了如何仅使用堆栈(标准操作为压栈、弹栈、获取栈顶元素、判断是否为空)实现队列的基本操作:入队、出队、获取队首元素、判断队列是否为空。通过在代码中模拟输入堆栈和输出堆栈的使用,当需要进行出队或获取队首元素操作时,若输出堆栈为空,则将输入堆栈的所有元素移动到输出堆栈中,以确保操作的正确顺序。

【题目】

Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.
Notes:
  • You must use only standard operations of a stack -- which means only push to toppeek/pop from topsize, and is empty operations are valid.
  • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

【思路】

I have one input stack, onto which I push the incoming elements, and one output stack, from which I peek/pop. I move elements from input stack to output stack when needed, i.e., when I need to peek/pop but the output stack is empty. When that happens, I move all elements from input to output stack, thereby reversing the order so it's the correct order for peek/pop.

The loop in peek does the moving from input to output stack. Each element only ever gets moved like that once, though, and only after we already spent time pushing it, so the overall amortized cost for each operation is O(1).

太巧妙了!

【代码】

class MyQueue {

    Stack<Integer> input = new Stack();
    Stack<Integer> output = new Stack();

    public void push(int x) {
        input.push(x);
    }

    public void pop() {
        peek();
        output.pop();
    }

    public int peek() {
//哇塞。。。这里简直太巧妙了。
        if (output.empty())
            while (!input.empty())
                output.push(input.pop());
        return output.peek();
    }

    public boolean empty() {
        return input.empty() && output.empty();
    }
}



class MyQueue {
    // Push element x to the back of queue.
    private Stack<Integer> one = new Stack<>();
    private Stack<Integer> two = new Stack<>();
    private int  count = 0;
    public void push(int x) {
        one.push(x);
        count++;
    }

    // Removes the element from in front of queue.
    public void pop() {
        int tmp = 0;
        if(empty()) return;
        for(int i = 0 ; i<count - 1;i++){
            tmp = one.pop();
            two.push(tmp);
        }
        int result = one.pop();
        while(!two.isEmpty()){
            tmp = two.pop();
            one.push(tmp);
        }
        count--;
    }

    // Get the front element.
    public int peek() {
         int tmp = 0;
        if(empty()) return 0;
        for(int i = 0 ; i<count - 1;i++){
            tmp = one.pop();
            two.push(tmp);
        }
        int result = one.peek();
        while(!two.isEmpty()){
            tmp = two.pop();
            one.push(tmp);
        }
        return result;
    }

    // Return whether the queue is empty.
    public boolean empty() {
        return (count == 0);

    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值