代码跟学Day10【代码随想录】

栈和队列

大纲

今日任务:
● 理论基础
● 232.用栈实现队列
● 225. 用队列实现栈

leetcode 232 用栈实现队列

思路

  • 用栈实现队列 栈是后进先出 队列是先进先出
  • 进队列的模拟直接和进栈一样 出队列的模拟是把当前栈的元素弹出到另一个栈再从另一个栈栈顶弹出

细节

  • 需要注意第二个栈如果内容不空的话就不必把第一个栈中元素弹出 而是等第二个栈清空后再考虑第一个栈中的情况

代码

class MyQueue {

    Stack<Integer> s1;
    Stack<Integer> s2;

    public MyQueue() {
        s1 = new Stack<>();
        s2 = new Stack<>();
    }
    
    public void push(int x) {
        s1.push(x);
    }
    
    public int pop() {
        transfer();
        return s2.pop();
    }
    
    public int peek() {
        transfer();
        return s2.peek();
    }
    
    public boolean empty() {
        return s1.isEmpty() && s2.isEmpty();
    }

    public void transfer() {
        if (s2.isEmpty())
            while (!s1.isEmpty()) 
                s2.push(s1.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();
 */

复杂度

  • 时间 push top O(n) pop peek O(1)
  • 空间 O(n)

leetcode 225 用队列实现栈

思路

  • 两个队列模拟一个栈 两个队列分主次 进栈无需模拟 出栈将当前元素除最后一个外全部挪出去到次队列 取出此值 再将辅助队列赋给主队列
  • 一个队列模拟一个栈 进栈照常 出栈只需要把当前队列除最后一个元素全部重新入队列 然后弹出队列头元素即为出栈

细节

  • 注意借助方法size判断当前队列元素个数

代码

  • 两个队列
class MyStack {

    Queue<Integer> q1;
    Queue<Integer> q2;
    
    public MyStack() {
        q1 = new LinkedList<>();
        q2 = new LinkedList<>();
    }
    
    public void push(int x) {
        q1.offer(x);
    }
    
    public int pop() {
        int size = q1.size();
        while (size-- > 1) {
            q2.offer(q1.poll());
        }
        int res = q1.poll();
        Queue<Integer> tmp = q1;
        q1 = q2;
        q2 = tmp;
        return res;
    }
    
    public int top() {
        int size = q1.size(), res = 0;
        while (size-- > 0) {
            res = q1.poll();
            q2.offer(res);
        }
        Queue<Integer> tmp = q1;
        q1 = q2;
        q2 = tmp;
        return res;
    }
    
    public boolean empty() {
        return q1.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<Integer> queue;

    public MyStack() {
        queue = new LinkedList<>();
    }
    
    public void push(int x) {
        queue.offer(x);
        int size = queue.size();
        while (size-- > 1) {
            queue.offer(queue.poll());
        }
    }
    
    public int pop() {
        return queue.poll();
    }
    
    public int top() {
        return queue.peek();
    }
    
    public boolean empty() {
        return queue.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();
 */

复杂度

  • 两个队列
    • 时间 push top O(n) 其它O(1)
    • 空间 O(n)
  • 一个队列
    • 时间 push O(n) 其它O(1)
    • 空间 O(n)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值