kotlin实现LRUCache

与一般的结构不同,参考了LinkedHashMap,将经常访问的放在最后,linkedLast()

相关题目,https://leetcode.cn/problems/lru-cache/

kotlin 复制代码
class MyLRUCache(private val maxSize: Int) {
    class Node(
        val key: Int, var value: Int, var before: Node? = null, var after: Node? = null
    )

    private var head: Node? = null
    private var tail: Node? = null
    private val map = HashMap<Int, Node>()

    fun getAndPrintln(key: Int) = get(key).also {
        println("get ${key}, ${this}")
    }

    private fun linkLast(n: Node) {
        if (tail == null) {
            head = n
            tail = head
            return
        }
        tail!!.after = n
        n.before = tail
        tail = n
    }

    private fun removeFirst() {
        if (head == null) {
            return
        }
        head = head!!.after
        head!!.before = null
    }

    private fun linkRemove(n: Node) {
        if (n == head) {
            head = head!!.after
        }
        if (n == tail) {
            tail = tail!!.before
        }
        n.before?.after = n.after
        n.after?.before = n.before
        n.before = null
        n.after = null
    }

    fun putAndPritln(key: Int, value: Int) = put(key, value).also {
        println("put ${key},${value}, ${this}")
    }

    fun put(key: Int, value: Int) {
        // 存在
        if (key in map) {
            val n = map[key]!!
            n.value = value
            if (n == tail) {
                return
            }
            linkRemove(n)
            linkLast(n)
            return
        }
        // 新建
        val n = Node(key, value)
        map[key] = n
        linkLast(n)
        if (map.size > maxSize) {
            map -= head!!.key
            removeFirst()
        }
    }

    fun remove(key: Int): Int {
        // 不存在
        if (key !in map) {
            return -1
        }
        val n = map.remove(key)!!
        linkRemove(n)
        return n.value
    }

    fun get(key: Int): Int {
        if (key !in map) {
            return -1
        }
        val n = map[key]!!
        linkRemove(n)
        linkLast(n)
        return n.value
    }

    override fun toString() = StringBuilder().apply {
        append("[")
        var n = head
        while (n != null) {
            append(
                if (n == head) "" else ", "
            )
            append("${n.key}=${n.value}")
            n = n.after
        }
        append("]")
    }.toString()
}
相关推荐
fundroid16 小时前
Kotlin 泛型进阶:in、out 与 reified 实战
android·开发语言·kotlin
JMchen1231 天前
现代Android图像处理管道:从CameraX到OpenGL的60fps实时滤镜架构
android·图像处理·架构·kotlin·android studio·opengl·camerax
JMchen1233 天前
Android CameraX深度解析:从Camera1到CameraX的相机架构演进
android·java·数码相机·架构·kotlin·移动开发·android-studio
倔强的石头1063 天前
【Linux指南】进程控制系列(五)实战 —— 微型 Shell 命令行解释器实现
linux·运维·kotlin
Hz4533 天前
Android Jetpack核心组件协同实战:Navigation 3.X+Lifecycle+Flow+Hilt的架构革新
android·kotlin
JMchen1233 天前
Android音频编码原理与实践:从AAC到Opus,深入解析音频编码技术与移动端实现
android·经验分享·学习·kotlin·android studio·音视频·aac
JMchen1234 天前
Android音频处理全解析:从3A算法到空间音频,打造专业级音频体验
android·经验分享·算法·kotlin·android studio·音视频
瓦特what?4 天前
C++编程防坑指南(小说版)
android·c++·kotlin
一招定胜负4 天前
卷积神经网络提取人脸五个特征点
人工智能·cnn·kotlin
HeDongDong-4 天前
详解 Kotlin 的函数
开发语言·python·kotlin