首页 > 其他 > 详细

232. Implement Queue using Stacks

时间:2020-09-26 09:11:56      阅读:41      评论:0      收藏:0      [点我收藏+]
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()
 */

 

232. Implement Queue using Stacks

原文:https://www.cnblogs.com/johnnyzhao/p/13732916.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!