(补卡)代码随想录算法训练营第十天 | 232. 用栈实现队列、225. 用队列实现栈.

232. 用栈实现队列

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

实现 MyQueue 类:

  • void push(int x) 将元素 x 推到队列的末尾
  • int pop() 从队列的开头移除并返回元素
  • int peek() 返回队列开头的元素
  • boolean empty() 如果队列为空,返回 true ;否则,返回 false

文章讲解:代码随想录

视频讲解:栈的基本操作! | LeetCode:232.用栈实现队列_哔哩哔哩_bilibili

第一印象:这是考察对栈和队列的理解和基础操作,栈是先进后出,而队列是先进先出,所以两者相互实现都需要两个栈或者两个队列。

看完讲解:细节上的操作注意一下。

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() {
        dumpStackIn();
        return stackOut.pop();
    }
    public int peek() {
        dumpStackIn();
        return stackOut.peek();
    }
    //  判断是否为空
    public boolean empty() {
        return stackIn.isEmpty() && stackOut.isEmpty();
    }
    //  如果stackOut不为空则直接输出stackOut,如果stackOut为空则将stackIn输入到stackOut中
    private void dumpStackIn() {
        if (!stackOut.isEmpty()) return;
        while (!stackIn.isEmpty()) {
            stackOut.push(stackIn.pop());
        }
    }
}

225. 用队列实现栈

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(pushtoppop 和 empty)。

实现 MyStack 类:

  • void push(int x) 将元素 x 压入栈顶。
  • int pop() 移除并返回栈顶元素。
  • int top() 返回栈顶元素。
  • boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

文章讲解:代码随想录

视频讲解:队列的基本操作! | LeetCode:225. 用队列实现栈_哔哩哔哩_bilibili

第一印象:和上一题是相同的。

看完讲解:还是自己想的少了,本质上还是有区别的,有多种操作来实现。

使用两个Queue来实现:

//  使用两个Queue来实现栈
class MyStack {
    Queue<Integer> queue1;
    Queue<Integer> queue2;
    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }
    //  进栈操作
    public void push(int x) {
        queue2.offer(x);  //  add方法和offer方法的区别在于,list一般用add而queue用offer
        while (!queue1.isEmpty()) {
            queue2.offer(queue1.poll());
        }
        Queue<Integer> temp = new LinkedList<>();
        temp = queue1;
        queue1 = queue2;
        queue2 = temp;
    }
    //  出栈操作
    public int pop() {
        return queue1.poll();
    }
    //  获取栈顶元素
    public int top() {
        return queue1.peek();
    }
    //  判断栈是否为空
    public boolean empty() {
        return queue1.isEmpty();
    }
}

使用两个Deque来实现:

//  使用两个Deque实现
class MyStack {
    Deque<Integer> que1;
    Deque<Integer> que2;
    public MyStack() {
        que1 = new ArrayDeque<>();
        que2 = new ArrayDeque<>();
    }
    //  进栈操作
    public void push(int x) {
        que1.addLast(x);
    }
    //  出栈操作
    public int pop() {
        int size = que1.size();
        size--;
        while (size-- > 0) {
            que2.addLast(que1.peekFirst());
            que1.pollFirst();
        }
        int res = que1.pollFirst();
        que1 = que2;
        que2 = new ArrayDeque<>();
        return res;
    }
    //  检查栈顶
    public int top() {
        return que1.peekLast();
    }
    //  检查是否为空
    public boolean empty() {
        return que1.isEmpty();
    }
}

使用单个Queue来实现:

//  使用单个Queue来实现
class MyStack {
    Queue<Integer> queue;
    public MyStack() {
        queue = new LinkedList<>();
    }
    //  进栈操作
    public void push(int x) {  //  插入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();
    }
}

使用单个Deque来实现:

//  使用单个Deque来实现
class MyStack {
    Deque<Integer> queue;
    public MyStack() {
        queue = new ArrayDeque<>();
    }
    //  进栈操作
    public void push(int x) {  //  插入x并将其放到队首
       queue.addLast(x);
    }
    //  出栈操作
    public int pop() {
        int size = queue.size();
        size--;
        while (size-- > 0) {
            queue.addLast(queue.peekFirst());
            queue.pollFirst();
        }
        int res = queue.pollFirst();
        return res;
    }
    //  检查栈顶
    public int top() {
        return queue.peekLast();
    }
    //  检查栈是否为空
    public boolean empty() {
        return queue.isEmpty();
    }
}

今日小结:今天对队列的学习还是十分全面的,补全了自己很多的小知识,然后一道题的多种解答练习也是让自己更加熟悉了队列的操作,这对我来说是很好的练习和进步,希望再接再厉吧。明天也要加油!

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值