622. Design Circular Queue

复制代码
package LeetCode_622

/**
 * 622. Design Circular Queue
 * https://leetcode.com/problems/design-circular-queue/description/
 *
 * Your implementation should support following operations:
MyCircularQueue(k): Constructor, set the size of the queue to be k.
Front: Get the front item from the queue. If the queue is empty, return -1.
Rear: Get the last item from the queue. If the queue is empty, return -1.
enQueue(value): Insert an element into the circular queue. Return true if the operation is successful.
deQueue(): Delete an element from the circular queue. Return true if the operation is successful.
isEmpty(): Checks whether the circular queue is empty or not.
isFull(): Checks whether the circular queue is full or not.
 * */
class MyCircularQueue(k: Int) {
  var array: IntArray? = null
    var size = 0
    var currentSize = 0
    var front = -1
    //rear always pointed to the available location where can insert new data
    var rear = -1

    /** Initialize your data structure here. Set the size of the queue to be k. */
    init {
        this.size = k
        array = IntArray(size)
    }

    /** Insert an element into the circular queue. Return true if the operation is successful. */
    fun enQueue(value: Int): Boolean {
        if (isFull()) {
            return false
        }
        rear = (rear + 1) % size //for example: 2%5=2, -2%5=-2, 2%-5=2
        array!![rear] = value
        currentSize++
        if (front == -1) {
            front = rear
        }
        return true
    }

    /** Delete an element from the circular queue. Return true if the operation is successful. */
    fun deQueue(): Boolean {
        if (isEmpty()) {
            return false
        }
        //move the front to next
        //val deQeueuElement = array!![front]
        front = (front + 1) % size
        currentSize--
        return true
    }

    /** Get the front item from the queue. */
    fun Front(): Int {
        return if (isEmpty()) -1 else array!![front]
    }

    /** Get the last item from the queue. */
    fun Rear(): Int {
        return if (isEmpty()) -1 else array!![rear]
    }

    /** Checks whether the circular queue is empty or not. */
    fun isEmpty(): Boolean {
        return currentSize == 0
    }

    /** Checks whether the circular queue is full or not. */
    fun isFull(): Boolean {
        return currentSize == array?.size
    }
}

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * var obj = MyCircularQueue(k)
 * var param_1 = obj.enQueue(value)
 * var param_2 = obj.deQueue()
 * var param_3 = obj.Front()
 * var param_4 = obj.Rear()
 * var param_5 = obj.isEmpty()
 * var param_6 = obj.isFull()
 */
复制代码

 

posted @   johnny_zhao  阅读(87)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示