ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

高频刷题-232. Implement Queue using Stacks

2021-05-30 16:00:07  阅读:229  来源: 互联网

标签:pop public Queue stackBack isEmpty push using Implement stack


https://leetcode.com/problems/implement-queue-using-stacks/

 

用两个stack(LIFO)去实现队列(FIFO), 这两种结构是完全相反的,所以需要一个辅助的stack将入栈的数据给倒置以下,最先想到的办法是实现push,首先导致当前栈stack的元素到back栈中,然后push当前的元素,再从back中导入回来:

这样使得pop(), peek都非常简单。

实现代码如下:

class MyQueue {

    private Stack<Integer> stack;
    private Stack<Integer> stackBack;

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

    /**
     * Push element x to the back of queue.
     */
    public void push(int x) {
        while (!stack.isEmpty()) stackBack.push(stack.pop());
        stack.push(x);
        while (!stackBack.isEmpty()) stack.push(stackBack.pop());
    }

    /**
     * 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();
    }
}

但是push()操作每次的时间复杂度都是O(n),再看题目的要求:

Follow-up: Can you implement the queue such that each operation is amortized O(1) time complexity?

需要实现摊还时间复杂度O(1)。 啥是摊还时间复杂度?

摊还分析给出了所有操作的平均性能。摊还分析的核心在于,最坏情况下的操作一旦发生了一次,那么在未来很长一段时间都不会再次发生,这样就会均摊每次操作的代价。

这样我们可以这样实现,push的时候,都push到stack中,而pop的时候,去判断stackBack中的元素是否为空,如果为空,需要翻转一次(从stack pop所有元素到stackBack中),如果stackBack不为空,说明可以从栈中直接pop元素(因为已经是之前翻转过的)。Peek的时候也做类似操作。这样N次操作均摊下来的时间复杂度为O(1)。

代码实现如下:

class MyQueue {

    private Stack<Integer> stack;
    private Stack<Integer> stackBack;

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

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

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

    /**
     * Get the front element.
     */
    public int peek() {
        if (stackBack.isEmpty()) {
            while (!stack.isEmpty()) stackBack.push(stack.pop());
        }
        return stackBack.peek();
    }

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

 

标签:pop,public,Queue,stackBack,isEmpty,push,using,Implement,stack
来源: https://blog.csdn.net/wangpu_baggio/article/details/117399103

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有