0
点赞
收藏
分享

微信扫一扫

Django当DEBUG = False时,信号不触发问题

星河出山 2023-07-19 阅读 53

题目

思路

代码

class MyQueue {
    public Stack<Integer> stack_1;
    public Stack<Integer> stack_2;

    public MyQueue() {
         stack_1 = new Stack<>();
         stack_2 = new Stack<>();
    }

    public void push(int x) {
        this.stack_1.push(x);
    }

    public int pop() {
        // 输出栈为空
        // 则将输入栈的数据复制一份到输出栈中
        while (stack_2.empty()){
            while (!stack_1.empty()){
                stack_2.push(stack_1.pop());
            }
        }
        return stack_2.pop();
    }

    public int peek() {
        while (stack_2.empty()){
            while (!stack_1.empty()){
                stack_2.push(stack_1.pop());
            }
        }
        return stack_2.peek();
    }

    public boolean empty() {
        while (stack_2.empty() && stack_1.empty()){
            return true;
        }
        return false;

    }
}

题目

思路

代码

class MyStack {
    Queue<Integer> q1;
    Queue<Integer> q2;

    public MyStack() {
        q1 = new LinkedList<>();
        q2 = new LinkedList<>();
    }

    public void push(int x) {
        q2.add(x);
        //循环后 q1的队列为空都加入到q2中了
        while (!q1.isEmpty()){
            q2.add(q1.poll());
        }
        // 交换q1、q2 保证q2在下次加入队列时为空 q1中保持与栈一致
        // 这样向q2加入数据时
        // 该数据就是q2的第一个 保持与栈一致
        Queue<Integer> temp ;
        temp = q1;
        q1 = q2;
        q2 = temp;
    }

    public int pop() {
        return q1.poll();
    }

    public int top() {
        return q1.peek();
    }

    public boolean empty() {
        return q1.isEmpty();

    }
}
举报

相关推荐

0 条评论