刷题第十天 | 232.用栈实现队列、225. 用队列实现栈

232. Implement Queue using Stacks

题目链接:232.用栈实现队列
思路链接:代码随想录栈与队列-用栈实现队列

思路

使用两个栈模拟队列,一个为输入栈,另一个为输出栈。push时进入输入栈,pop时如果输出栈为空,则将输入栈的所有元素pop出来,push进入输出栈,然后再从输出栈pop。
注意一定要等输出栈为空才能将输入栈的元素加入到输出栈,否则顺序会出现错误。
尽量复用函数,这是一个好习惯!

心路历程

不难,在学cs61b时做到过了

Code

class MyQueue {
    private Stack<Integer> stackIn;
    private 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()) {
            while (!stackIn.isEmpty()) {
                stackOut.push(stackIn.pop());
            }
        }
        return stackOut.pop();
    }
    
    public int peek() {
        // if (stackOut.isEmpty()) {
        //     while (!stackIn.isEmpty()) {
        //         stackOut.push(stackIn.pop());
        //     }
        // }
        // return stackOut.peek();
        
        // 也可以通过复用pop()实现
        int popped = this.pop();
        stackOut.push(popped); // 要push回stackOut里
        return popped;
    }
    
    public boolean empty() {
        return stackOut.isEmpty() && stackIn.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();
 */

225. Implement Stack using Queues

题目链接:225. 用队列实现栈
思路链接:代码随想录栈与队列-用队列实现栈

思路

用两个队列实现:第一个为输出队列,第二个队列的作用就是缓存。push时add入输出队列;当pop时poll掉size - 1个输出队列的元素,add到缓存队列内,然后再从缓存队列poll出最后一个元素即为返回值。top时就复用pop函数就行,再重新加回去。

用一个队列实现:push就直接add;pop时poll掉size - 1个输出队列的元素,再重新add到队列里,下一个poll得到的值就是return值。top无非就是复用pop函数,再重新加回去。

心路历程

不难

Code

class MyStack {
    // 两个queues方法
    private Queue<Integer> queueOut;
    private Queue<Integer> queueStore;
    
    public MyStack() {
        queueOut = new ArrayDeque<>();
        queueStore = new ArrayDeque<>();
    }
    
    public void push(int x) {
        queueOut.add(x);
    }
    
    public int pop() {
        int result = Integer.MAX_VALUE;
        while (!queueOut.isEmpty()) {
            result = queueOut.poll();
            queueStore.add(result);
        }
        while (queueStore.size() > 1) {
            queueOut.add(queueStore.poll());
        }
        queueStore.poll();
        return result;
    }
    
    public int top() {
        int result = this.pop();
        queueOut.add(result);
        return result;
    }
    
    public boolean empty() {
        return queueOut.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();
 */
class MyStack {
    //  一个queue的方法
    private Queue<Integer> q;
    
    public MyStack() {
        q = new ArrayDeque<>();    
    }
    
    public void push(int x) {
        q.add(x);
    }
    
    public int pop() {
        int size = q.size();
        size--;
        while (size != 0) {
            int polled = q.poll();
            q.add(polled);
            size--;
        }
        return q.poll();
    }
    
    public int top() {
        int result = this.pop();
        q.add(result);
        return result;
    }
    
    public boolean empty() {
        return q.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();
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值