训练营第九天(栈与队列第一部分)

文章讲述了如何使用栈和队列的数据结构来实现队列和栈的基本操作,包括push、pop、peek和empty,以及在Java中使用LinkedList、PriorityQueue和ArrayDeque的不同模拟方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

训练营第九天

232.用栈实现队列

力扣题目链接(opens new window)

题目

使用栈实现队列的下列操作:

push(x) – 将一个元素放入队列的尾部。
pop() – 从队列首部移除元素。
peek() – 返回队列首部的元素。
empty() – 返回队列是否为空。

示例:

MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek();  // 返回 1
queue.pop();   // 返回 1
queue.empty(); // 返回 false

1
2
3
4
5
6

说明:

  • 你只能使用标准的栈操作 – 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
  • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
  • 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。

解答

就是两个栈,一个进,一个出,也就相当于一个队列

class MyQueue {

    private Stack<Integer> stackIn;//正常入栈
    private Stack<Integer> stactkOut;//出栈,此时的栈头也就相当于出队列
    public MyQueue() {
        stackIn = new Stack<>();
        stactkOut = new Stack<>();
    }
    
    public void push(int x) {
        stackIn.push(x);
    }
    
    public int pop() {
        inToOut();
        return stactkOut.pop();//out的栈头也就是队头
    }
    
    public int peek() {
        inToOut();
        return stactkOut.peek();
    }
    
    public boolean empty() {
        return stackIn.isEmpty() && stactkOut.isEmpty();
    }

    // 如果stackOut为空,那么将stackIn中的元素全部放到stackOut中
    public void inToOut(){
        if (!stactkOut.isEmpty())
            return;
        //因为如果不为空,就不应该再放,而是应该等out全部出栈后再进行放入,因为out不为空,此时的栈头也就是队列的对头
        while (!stackIn.isEmpty())
            stactkOut.push(stackIn.pop());
    }
}

225. 用队列实现栈

力扣题目链接(opens new window)

题目

使用队列实现栈的下列操作:

  • push(x) – 元素 x 入栈
  • pop() – 移除栈顶元素
  • top() – 获取栈顶元素
  • empty() – 返回栈是否为空

注意:

  • 你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。
  • 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
  • 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。

解答

Queue

Queue是java中实现队列的接口

Queue的实现类有LinkedListPriorityQueue。最常用的实现类是LinkedList。

Queue的6个方法分类:

  • 压入元素(添加):add()、offer()

    • 相同:未超出容量,从队尾压入元素,返回压入的那个元素。
    • 区别:在超出容量时,add()方法会对抛出异常,offer()返回false
  • 弹出元素(删除):remove()、poll()

    • 相同:容量大于0的时候,删除并返回队头被删除的那个元素。
    • 区别:在容量为0的时候,remove()会抛出异常,poll()返回false
  • 获取队头元素(不删除):element()、peek()

    • 相同:容量大于0的时候,都返回队头元素。但是不删除。
    • 区别:容量为0的时候,element()会抛出异常,peek()返回null。
抛出异常返回特殊值
插入add(e)offer(e)
删除remove()poll()
获取对头element()peek()
Dueue

常常使用ArrayDeque定义

作为队列

双端队列,既能实现栈也能实现队列,底层由数组实现,要比LinkedList效率快很多,也有队列中的方法,方法与结果都与其相同,不同的是多了几个方法可以使其像栈一样

2. 使用 offer(),offerFirst()和offerLast()插入元素

  • offer() - 将指定的元素插入ArrayDeque双端队列的末尾 (队列)
  • offerFirst() -在ArrayDeque双端队列的开头,插入指定的元素**(相当于将元素压入栈中)**
  • offerLast() - 等效于offer( ) (队列)

4. 使用peek(),peekFirst()和peekLast()方法访问元素

  • peek() - 等价于找队头
  • peekFirst() - 等价于找队头
  • peekLast() -等价于栈顶

5. 使用poll(),pollFirst()和pollLast()方法删除元素

  • poll() - 返回并删除ArrayDeque双端队列的第一个元素**(队列)**
  • pollFirst() - 返回并删除ArrayDeque双端队列的第一个元素**(队列)**
  • pollLast() - 返回并删除ArrayDeque双端队列的最后一个元素**(栈,弹出栈头)**
作为栈
  • push() - 在堆栈顶部添加一个元素(向数组的头部加)
  • peek() - 从堆栈顶部返回一个元素(找数组的尾部)
  • pop() - 返回并从堆栈顶部删除元素
思路一

使用两个队列,每次压入元素时先放入辅助队列中,然后再将主队列中元素依次放入辅助队列中,最后调换辅助队列与主队列,每次压入的元素都是栈头,也就保证了每次都是相反的顺序

class MyStack {
    Queue<Integer> queue1;//主队列
    Queue<Integer> queue2;//辅助队列
    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }
    
    public void push(int x) {
        queue2.offer(x);
        while (!queue1.isEmpty())
            queue2.offer(queue1.poll());
        Queue<Integer> queueTemp;
        queueTemp = queue1;
        queue1 = queue2;
        queue2 = queueTemp; // 最后交换queue1和queue2,将元素都放到queue1中,并且队头就是永远是最后放入的元素
    }
    
    public int pop() {
        return queue1.poll(); // 因为queue1中的元素和栈中的保持一致
    }
    
    public int top() {
        return queue1.peek();
    }
    
    public boolean empty() {
        return queue1.isEmpty();
    }
    
}
思路二(没意义)

使用一个双端队列来实现

class MyStack {
    Deque<Integer> que1;
    //Queue中的 add、poll、peek等效于 Deque 中的 addLast、pollFirst、peekFirst
    //而addFirst、pollLast、peekLast也就等价于栈
    public MyStack() {
        que1 = new ArrayDeque<>();
    }
    
    public void push(int x) {
        que1.addLast(x);
    }
    
    public int pop() {
        return que1.pollLast();
    }
    
    public int top() {
        return que1.peekLast();
    }
    
    public boolean empty() {
        return que1.isEmpty();
    }

}
思路三

使用一个队列实现

但是要注意当不取对头的时候,使用了removeLastToTop()后,会导致当前的对头已经是栈顶,那么在pop的时候就会出现错误,所以在top方法中移动后一定要将当前元素重新入队,防止再次pop时出现问题

class MyStack {
    Queue<Integer> que1;
    public MyStack() {
        que1 = new LinkedList<>();
    }
    
    public void push(int x) {
        que1.offer(x);
    }
    
    public int pop() {
        removeLastToTop();
        return que1.poll();
    }
    
    public int top() {
        removeLastToTop();
        int result = que1.poll();
        que1.add(result);
        return result;
    }
    
    public boolean empty() {
        return que1.isEmpty();
    }

    public void removeLastToTop(){
        int size = que1.size() - 1;//留最后一个元素作为队头,模拟为栈顶
        while (size-- > 0)
            que1.offer(que1.poll());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值