代码随想录算法训练营day10 | 232.用栈实现队列 225. 用队列实现栈

114 阅读4分钟

232.用栈实现队列

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

  • void push(int x) 将元素 x 推到队列的末尾
  • int pop() 从队列的开头移除并返回元素
  • int peek() 返回队列开头的元素
  • boolean empty() 如果队列为空,返回 true ;否则,返回 `false 说明:
  • 你 只能 使用标准的栈操作 —— 也就是只有 push to toppeek/pop from topsize, 和 is empty 操作是合法的。
  • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
  • 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)

232. 用栈实现队列 - 力扣(Leetcode)

思路

栈的特点是后进先出,队列的特点的是先进先出,使用两个栈来实现队列,将其中一个栈称为存储栈,另一个栈称为辅助栈。

  • 入队:入队时在存储栈的栈底插入元素。
  • 出队:出队时依次将元素从存储栈的栈顶取出,加入到辅助栈的栈尾中,直到存储栈的栈空;而后将辅助栈的栈顶元素出栈并保存,作为出队操作的返回值;最后依次将元素从辅助的栈顶取出,加入到存储栈的栈尾中,直到辅助栈的栈空。
  • 查看队首元素:依次将元素从存储栈的栈顶取出,加入到辅助栈的栈尾中,直到存储栈的栈空;而后保存辅助栈的栈顶元素不出栈,作为本操作的返回值;最后依次将元素从辅助的栈顶取出,加入到存储栈的栈尾中,直到辅助栈的栈空。

代码

class MyQueue {

    private Deque<Integer> stack1,stack2;

    public MyQueue() {
        stack1=new LinkedList<>();
        stack2=new LinkedList<>();
    }
    
    public void push(int x) {
        stack1.push(x);
    }
    
    public int pop() {

        while(!stack1.isEmpty()){
            stack2.push(stack1.pop());
        }

        Integer res=stack2.pop();

        while(!stack2.isEmpty()){
            stack1.push(stack2.pop());
        }

        return res;

    }
    
    public int peek() {

        while(!stack1.isEmpty()){
            stack2.push(stack1.pop());
        }

        Integer res=stack2.peek();

        while(!stack2.isEmpty()){
            stack1.push(stack2.pop());
        }

        return res;

    }
    
    public boolean empty() {
        return stack1.isEmpty()?true:false;
    }
}

/**
 * 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. 用队列实现栈

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

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

注意:

  • 你只能使用队列的基本操作 —— 也就是 push to backpeek/pop from frontsize 和 is empty 这些操作。
  • 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。

225. 用队列实现栈 - 力扣(Leetcode)

思路

使用两个队列来实现栈,其中一个队列称为存储队列,另一个队列称为辅助队列。

  • 入栈:直接在存储队列队尾插入元素。
  • 出栈:首先依次将元素从存储队列的队首取出,插入到辅助队列的队尾中,直到存储队列中只剩下一个元素;而后将存储队列的最后一个元素取出,并保存,作为出栈操作的返回值;最后,依次将元素从辅助队列的队首取出,插入到存储队列的队尾中,直到辅助队列为空。
  • 查看栈顶元素:首先依次将元素从存储队列的队首取出,插入到辅助队列的队尾中,直到存储队列中只剩下一个元素;而后将存储队列的最后一个元素取出,并保存,作为出栈操作的返回值;然后,依次将元素从辅助队列的队首取出,插入到存储队列的队尾中,直到辅助队列为空;最后,将之前保存的此次操作的返回值插入到存储队列的队尾

代码

class MyStack {

    Deque<Integer> que1,que2;

    public MyStack() {
        que1=new LinkedList<>();
        que2=new LinkedList<>();
    }
    
    public void push(int x) {
        que1.add(x);
    }
    
    public int pop() {
        while(que1.size()>1){
            que2.add(que1.poll());
        }

        int res=que1.poll();

        while(!que2.isEmpty()){
            que1.add(que2.poll());
        }

        return res;
    }
    
    public int top() {
        while(que1.size()>1){
            que2.add(que1.poll());
        }

        int res=que1.poll();

        while(!que2.isEmpty()){
            que1.add(que2.poll());
        }

        que1.add(res);

        return res;
    }
    
    public boolean empty() {
        return que1.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();
 */

Java中的栈和队列

在java中,一般通过Deque接口的实现类LinkedList来使用栈和队列的功能。
Deque 支持两端元素插入和移除的线性集合。 名称deque是“双端队列”的缩写,通常发音为“deck”。

LinkedList对于栈和队列的基本操作如下:

队首/栈首元素
push()pop()peek()
队列add()poll()peek()