LeetCode-225.Implement Stack using Queues

Implement the following operations of a stack using queues.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • empty() -- Return whether the stack is empty.

Example:

MyStack stack = new MyStack();
stack.push(1);
stack.push(2);  
stack.top();   // returns 2
stack.pop();   // returns 2
stack.empty(); // returns false

Notes:

  • You must use only standard operations of a queue -- which means only push to backpeek/pop from frontsize, and is empty operations are valid.
  • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

使用两个队列

输入时间复杂度O(1),输出复杂度O(n)

 1 class MyStack {//栈 队列 my
 2     Queue<Integer> queue1 ;
 3     Queue<Integer> queue2 ;
 4     int top;
 5 
 6     /** Initialize your data structure here. */
 7     public MyStack() {
 8         queue1 = new LinkedList<Integer>();
 9         queue2= new LinkedList<Integer>();
10     }
11     
12     /** Push element x onto stack. */
13     public void push(int x) {
14         queue1.add(x);
15         top=x;
16     }
17     
18     /** Removes the element on top of the stack and returns that element. */
19     public int pop() {
20         if(queue1.isEmpty()){
21             while(1<queue2.size()){
22                 top = queue2.poll();
23                 queue1.add(top);
24             }
25             return queue2.poll();
26         }
27         else{
28             while(1<queue1.size()){
29                 top = queue1.poll();
30                 queue2.add(top);
31             }
32             return queue1.poll();
33         }
34     }
35     
36     /** Get the top element. */
37     public int top() {
38         return top;
39     }
40     
41     /** Returns whether the stack is empty. */
42     public boolean empty() {
43         return queue1.isEmpty()&&queue2.isEmpty();
44     }
45 }

 

可使用一个队列完成

class MyStack {
    Queue<Integer> q1 ;

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

 

相关题

使用栈实现队列 LeetCode232 https://www.cnblogs.com/zhacai/p/10565130.html

posted @ 2019-03-20 15:26  月半榨菜  阅读(108)  评论(0编辑  收藏  举报