码迷,mamicode.com
首页 > 其他好文 > 详细

232. Implement Queue using Stacks

时间:2015-11-27 14:36:37      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

题目:

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).

链接: http://leetcode.com/problems/implement-queue-using-stacks/

题解:

用栈实现队列。之前的做法是使用一个栈保存所有的数据,每次增加新数据之前先倒腾到另外一个栈里,再倒腾回来,这样的速度会很慢,只击败了13%的玩家...

仔细想了想,两个栈,一个用来push,另外一个专门用来pop。当两个栈都不为空的时候可以做到push和pop以及peek,isEmpty()都是O(1)。但最坏情况也会时间较长。 总的来说可以用平均时间取胜。代码还可以再优化优化。

Time Complexity -  push - O(n), pop - O(n), peek - O(n), isEmpty - O(1)。

class MyQueue {
    private Stack<Integer> stack1 = new Stack<>();
    private Stack<Integer> stack2 = new Stack<>();
    
    // Push element x to the back of queue.
    public void push(int x) {
        if(stack1.isEmpty()) {
            stack1.push(x);
        } else if (stack2.isEmpty()) {
            stack2.push(x);
            while(!stack1.isEmpty())
                stack2.push(stack1.pop());
        } else {
            stack1.push(x);
        }        
    }

    // Removes the element from in front of queue.
    public void pop() {
        if(!stack2.isEmpty()) {
            stack2.pop();
        } else if(!stack1.isEmpty()) {
            while(!stack1.isEmpty())
                stack2.push(stack1.pop());
            stack2.pop();
        }
    }

    // Get the front element.
    public int peek() {
        if(!stack2.isEmpty()) {
            return stack2.peek();
        } else if(!stack1.isEmpty()) {
            while(!stack1.isEmpty())
                stack2.push(stack1.pop());
            return stack2.peek();
        } else 
            return 0;
    }

    // Return whether the queue is empty.
    public boolean empty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }
}

 

Reference:

 https://leetcode.com/discuss/44106/short-o-1-amortized-c-java-ruby

https://leetcode.com/discuss/44124/accepted-0ms-c-solution-with-two-std-stack-easy-understand

https://leetcode.com/discuss/45146/accepted-clean-java-solution

https://leetcode.com/discuss/58346/my-java-solution-with-only-editing-the-push-method

https://leetcode.com/discuss/53948/java-solution-using-two-stacks

https://leetcode.com/discuss/47278/o-n-3o-1-solution-in-java

https://leetcode.com/discuss/67154/easy-java-solution-just-edit-push-method

https://leetcode.com/discuss/62282/my-java-solution-with-2-stacks

232. Implement Queue using Stacks

标签:

原文地址:http://www.cnblogs.com/yrbbest/p/5000307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!