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()
}
相关推荐
名剑走天下1 天前
Android 面试题大全
android·kotlin
chjif1 天前
Android 设备管控开发实战:自定义 Launcher 如何只显示指定 App?
android·kotlin
非凡ghost1 天前
用 Kotlin 和 Jetpack Compose 重写的安卓视频播放器,原生体验有多流畅?
android·java·kotlin·音视频·软件需求
名剑走天下2 天前
android 面试题
kotlin
蜡台2 天前
Jetpack Compose 稳定性、重组优化(Stable / @NonRestartableComposable)
android·kotlin·compose·jepack
JMchen1232 天前
Jetpack 内核实战(十):实战(下)——编辑页、全局设置与内存泄漏排查
kotlin·内存泄漏·android 实战·datastore 设置·jetpack 项目优化·编辑页开发
雨白3 天前
Kotlin 泛型基础:“万能魔法盒”的诞生
kotlin
小小测试开发3 天前
RAG评测:把忠实度、检索命中率、噪声敏感度做成可回归的流水线
人工智能·数据挖掘·回归·kotlin
撩得Android一次心动3 天前
Kotlin 语言【知识点整理3】
java·开发语言·笔记·学习·kotlin
Kapaseker3 天前
我常用的 5 个 Kotlin 优化小技巧
android·kotlin