美文网首页
力扣算法 - 用栈实现队列

力扣算法 - 用栈实现队列

作者: Mr_Bob_ | 来源:发表于2020-07-10 10:39 被阅读0次

    用栈实现队列

    题目

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

    push(x) -- 将一个元素放入队列的尾部。
    pop() -- 从队列首部移除元素。
    peek() -- 返回队列首部的元素。
    empty() -- 返回队列是否为空。
    
    示例:
    MyQueue queue = new MyQueue();
    
    queue.push(1);
    queue.push(2);  
    queue.peek();  // 返回 1
    queue.pop();   // 返回 1
    queue.empty(); // 返回 false
    
    说明:
    你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
    你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
    假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
    
    思路

    创建两个stack inStack outStack
    入队: 将 outStack 元素遍历,弹出栈顶元素 到 inStack, 在 inStack push进入新元素
    出队: 将 instack 元素遍历,弹出栈顶元素 到 outStack , 取出 outStack栈顶元素即可

    Java实现
    public class _232_用栈实现队列 {
        /** Initialize your data structure here. */
    
        public Stack<Integer> inStack = new Stack<>();
        public Stack<Integer> outStack = new Stack<>();
        
        public MyQueue() {
    
        }
    
        /** 入队 */
        public void push(int x) {
            while (!outStack.isEmpty()) {
                inStack.push(outStack.pop());
            }
            inStack.push(x);
        }
    
        /** 从队列首部移除元素 */
        public int pop() {
            while (!inStack.isEmpty()) {
                outStack.push(inStack.pop());
            }
            return outStack.pop();
        }
    
        /** 返回队列首部的元素 */
        public int peek() {
            while (!inStack.isEmpty()) {
                outStack.push(inStack.pop());
            }
            return outStack.peek();
        }
    
        /** 返回队列是否为空 */
        public boolean empty() {
            while (!inStack.isEmpty()) {
                outStack.push(inStack.pop());
            }
            return outStack.isEmpty();
        }
    }
    
    
    Swift实现
    class Test232 {
        
        var inStack = [Int]()
        var outStack = [Int]()
        
    
        /** Initialize your data structure here. */
        init() {
    
        }
        
        /** Push element x to the back of queue. */
        func push(_ x: Int) {
            while outStack.count > 0 {
                inStack.append(outStack.popLast()!)
            }
            inStack.append(x)
        }
        
        /** Removes the element from in front of queue and returns that element. */
        func pop() -> Int {
            while inStack.count > 0 {
                outStack.append(inStack.popLast()!)
            }
            return outStack.popLast()!
        }
        
        /** Get the front element. */
        func peek() -> Int {
            while inStack.count > 0 {
                outStack.append(inStack.popLast()!)
            }
            return outStack[outStack.count - 1]
        }
        
        /** Returns whether the queue is empty. */
        func empty() -> Bool {
            while inStack.count > 0 {
                outStack.append(inStack.popLast()!)
            }
            return outStack.count > 0 ? false : true
        }
    }
    
    

    相关文章

      网友评论

          本文标题:力扣算法 - 用栈实现队列

          本文链接:https://www.haomeiwen.com/subject/jlmpcktx.html