• 232. Implement Queue using Stacks


    package LeetCode_232
    
    import java.util.*
    
    /**
     * 232. Implement Queue using Stacks
     * https://leetcode.com/problems/implement-queue-using-stacks/description/
     *
     * Implement the following operations of a queue using stacks.
    push(x) -- Push element x to the back of queue.
    pop() -- Removes the element from in front of queue.
    peek() -- Get the front element.
    empty() -- Return whether the queue is empty.
     * */
    class MyQueue() {
    
        /** Initialize your data structure here. */
        val stack = Stack<Int>()
    
        /** Push element x to the back of queue. */
        fun push(x: Int) {
            stack.push(x)
        }
    
        /** Removes the element from in front of queue and returns that element. */
        fun pop(): Int {
            val firstElement = stack.get(0)
            stack.removeAt(0)
            return firstElement
        }
    
        /** Get the front element. */
        fun peek(): Int {
            return stack.get(0)
        }
    
        /** Returns whether the queue is empty. */
        fun empty(): Boolean {
            return stack.isEmpty()
        }
    }
    /**
     * Your MyQueue object will be instantiated and called as such:
     * var obj = MyQueue()
     * obj.push(x)
     * var param_2 = obj.pop()
     * var param_3 = obj.peek()
     * var param_4 = obj.empty()
     */
  • 相关阅读:
    怎样确定需求
    xampp进程和非进程执行
    将博客搬至CSDN
    管理心得
    数据库性能优化
    开发、架构总结
    activeMQ总结
    php数组操作函数
    Examples_08_08
    保险采购单的修复
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13732916.html
Copyright © 2020-2023  润新知