232. 用栈实现队列

题目:
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列的支持的所有操作(push、pop、peek、empty):

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

说明:
你只能使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

思路1:
和【225题:用队列实现栈】正好相反,可以结合刷。
了解栈和堆的数据结构。
栈:后进先出,顶端入栈,顶端出栈,好比弹夹。
队列:先进先出,后端入列,前端出列,好比水管。

代码如下:

/**
 * 
 * 232. 用栈实现队列
 *
 * 栈遵循后入先出的原则,从栈顶入,从栈顶出
 *
 * 队列遵循先进先出的原则,从后端入列,前端出列
 *
 * @author luxl
 * @date 2020年12月7日
 */
public class MyQueue {

    Stack<Integer> stack;

    /** Initialize your data structure here. */
    public MyQueue() {
        stack = new Stack<Integer>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        // 临时存放栈内元素
        Object[] arr = stack.toArray();
        stack.removeAll(Arrays.asList(arr));
        // 新入栈的元素应放于头部,取出时才会后取
        stack.add(x);
        for (Object o : arr) {
            stack.add((Integer) o);
        }
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        return stack.pop();
    }

    /** Get the front element. */
    public int peek() {
        return stack.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        MyQueue queue = new MyQueue();
        queue.push(1);
        queue.push(2);
        queue.push(3);
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
    }
}

代码如下:

/**
 * 
 * 232. 用栈实现队列
 *
 * 栈遵循后入先出的原则,从栈顶入,从栈顶出
 *
 * 队列遵循先进先出的原则,从后端入列,前端出列
 *
 * @author luxl
 * @date 2020年12月7日
 */
public class MyQueue1 {

    Stack<Integer> a;// 用于入栈
    Stack<Integer> b;// 用于出栈

    /** Initialize your data structure here. */
    public MyQueue1() {
        a = new Stack<Integer>();
        b = new Stack<Integer>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        a.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        // 如果b为空,a元素移至b
        if (b.isEmpty()) {
            while (!a.isEmpty()) {
                b.push(a.pop());
            }
        }
        if (!b.isEmpty()) {
            return b.pop();
        }
        return -1;
    }

    /** Get the front element. */
    public int peek() {
        // 如果b为空,a元素移至b
        if (b.isEmpty()) {
            while (!a.isEmpty()) {
                b.push(a.pop());
            }
        }
        if (!b.isEmpty()) {
            return b.peek();
        }
        return -1;
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return a.isEmpty() && b.isEmpty();
    }

}

-------------------------------小白学算法

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容