leetcode 225. Implement Stack using Queues 利用队列构建栈 ---------- java

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.

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

用队列构成栈,两种做法:1、一个队列实现,push 是O(n),其他是O(1)

2、两个队列实现,pop是O(n),其他事O(1)

class MyStack {

//one Queue solution
private Queue<Integer> q = new LinkedList<Integer>();

// Push element x onto stack.
public void push(int x) {
    q.add(x);
    for(int i = 1; i < q.size(); i ++) { //rotate the queue to make the tail be the head
        q.add(q.poll());
    }
}

// Removes the element on top of the stack.
public void pop() {
    q.poll();
}

// Get the top element.
public int top() {
    return q.peek();        
}

// Return whether the stack is empty.
public boolean empty() {
    return q.isEmpty();
}
}

2、

public class MyStack {

    private Queue<Integer> queue1;
    private Queue<Integer> queue2;
    private int num;
    
    /** Initialize your data structure here. */
    public MyStack() {
        queue1 = new LinkedList();
        queue2 = new LinkedList();
        num = 1;
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        if (num == 1){
            queue1.add(x);
        } else {
            queue2.add(x);
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        if (num == 1){
            int size = queue1.size();
            for (int i = 0; i < size - 1; i++){
                queue2.add(queue1.poll());
            }
            num = 2;
            return queue1.poll();
        } else {
            int size = queue2.size();
            for (int i = 0; i < size - 1; i++){
                queue1.add(queue2.poll());
            }
            num = 1;
            return queue2.poll();
        }
    }
    
    /** Get the top element. */
    public int top() {
        if (num == 1){
            int size = queue1.size();
            for (int i = 0; i < size - 1; i++){
                queue2.add(queue1.poll());
            }
            int top = queue1.poll();
            queue2.add(top);
            num = 2;
            return top;
        } else {
            int size = queue2.size();
            for (int i = 0; i < size - 1; i++){
                queue1.add(queue2.poll());
            }
            int top = queue2.poll();
            queue1.add(top);
            num = 1;
            return top;
        }  
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        if (num == 1){
            return queue1.isEmpty();
        } else {
            return queue2.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();
 */
原文地址:https://www.cnblogs.com/xiaoba1203/p/7160139.html