算法修炼Day10|● 232.用栈实现队列 ● 225. 用队列实现栈

69 阅读2分钟
题目:232. 用栈实现队列 - 力扣(LeetCode)
思路/想法:

队列:FIFO。 栈:FILO。 所以想要用栈(先进后出)实现队列的先进先出,则需要两个栈,一个起到临时存储和转换的作用,另一个则是起到存储结果和输出的作用。

代码实现:
// 方法一:
class MyQueue {
    Stack<Integer> stackIn;
    Stack<Integer> stackOut;
    public MyQueue() {
        stackIn = new Stack<>();
        stackOut = new Stack<>();
    }
    
    public void push(int x) {   
        stackIn.push(x);
    }
    
    public int pop() {
        if (!stackOut.isEmpty()) {
            return stackOut.pop();
        } else if (stackOut.isEmpty() && !stackIn.isEmpty()) {
            stackOut.push(stackIn.pop());
            while (!stackIn.isEmpty()) {
                stackOut.push(stackIn.pop());
            }
        }
        return stackOut.pop();
    }
    
    public int peek() {
        if (!stackOut.isEmpty()) {
            return stackOut.pop();
        } else if (stackOut.isEmpty() && !stackIn.isEmpty()) {
            stackOut.push(stackIn.pop());
            while (!stackIn.isEmpty()) {
                stackOut.push(stackIn.pop());
            }
        }
        return stackOut.peek();
    }
    
    public boolean empty() {
        return stackIn.isEmpty() && stackOut.isEmpty();
    }
}

/**
 * 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();
 */
 
 // 方法二:代码封装
 class MyQueue {
    Stack<Integer> stackIn;
    Stack<Integer> stackOut;
    public MyQueue() {
        stackIn = new Stack<>();
        stackOut = new Stack<>();
    }
    
    public void push(int x) {   
        stackIn.push(x);
    }
    
    public int pop() {
        valueTemp();
        return stackOut.pop();
    }
    
    public int peek() {
        valueTemp();
        return stackOut.peek();
    }
    
    public boolean empty() {
        return stackIn.isEmpty() && stackOut.isEmpty();
    }
    private void valueTemp() {
        if (!stackOut.isEmpty()) return;
        while (!stackIn.isEmpty()) {
            stackOut.push(stackIn.pop());
        }
    }
}

/**
 * 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. 用队列实现栈 - 力扣(LeetCode)
思路/想法:

方法一:使用Queue,两个队列实现栈。第一个队列维持为栈的数据存储形式,第二个队列辅助第一个队列维持栈的形态。 方法二:使用Deque双端队列。

代码实现:
// 方法一:两个队列实现
class MyStack {
    Queue<Integer> queue1;
    Queue<Integer> queue2;
    public MyStack() {
        // 实现类一:
        // queue1 = new LinkedList<>();
        // queue2 = new LinkedList<>();
        
        // 实现类二:数组似乎更快一些
        queue1 = new ArrayDeque<>();
        queue2 = new ArrayDeque<>();
    }
    // 以 queue1 为基准,queue2 为辅助队列。添加元素x时,先将 queue1 的元素移到 queue2 中,将元素添加到queue1 中,再将 queue2 的元素添加到 queue1 中,以此实现 queue1 内元素即是栈的空间形态。
    public void push(int x) {
        while (!queue1.isEmpty()) {
            queue2.add(queue1.poll());
        }
        queue1.add(x);
        while (!queue2.isEmpty()) {
            queue1.add(queue2.poll());
        }
    }
    
    public int pop() {
        return queue1.poll();
    }
    
    public int top() {
        return queue1.peek();
    }
    
    public boolean empty() {    
        return queue1.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();
 */
 // 方法二:Deque双端队列可以当作栈来使用!!
 class MyStack {
    Deque<Integer> deque = new ArrayDeque<>();
    public MyStack() {

    }
    
    public void push(int x) {
        deque.add(x);
    }
    
    public int pop() {
        return deque.pollLast();
    }
    
    public int top() {
        return deque.peekLast();
    }
    
    public boolean empty() {
        return deque.isEmpty();
    }
}