LeetCode讲解篇之面试题 16.25. LRU 缓存

面试题 16.25. LRU 缓存

文章目录

题目描述

设计和构建一个"最近最少使用"缓存,该缓存会删除最近最少使用的项目。缓存应该从键映射到值(允许你插入和检索特定键对应的值),并在初始化时指定最大容量。当缓存被填满时,它应该删除最近最少使用的项目。
它应该支持以下操作: 获取数据 get 和 写入数据 put 。
获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。

写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。
示例:

LRUCache cache = new LRUCache( 2 /* 缓存容量 */ );

cache.put(1, 1);

cache.put(2, 2);

cache.get(1); // 返回 1

cache.put(3, 3); // 该操作会使得密钥 2 作废

cache.get(2); // 返回 -1 (未找到)

cache.put(4, 4); // 该操作会使得密钥 1 作废

cache.get(1); // 返回 -1 (未找到)

cache.get(3); // 返回 3

cache.get(4); // 返回 4

解答思路

对于这道题,我们可以采用哈希表+双向链表的方式构造数据结构。哈希表保存key,value的映射,保证能在O(1)的时间复杂度中查找数据。双向链表保存操作的时间顺序,保证能在O(1)的时间复杂度中完成增加数据,删除数据,修改数据。我们每次增改查操作,将该节点移动至双向链表头部

解答代码

go 复制代码
type LRUCache struct {
    // 当前缓存的大小
    size int
    // 缓存最大的容量
    capacity int
    // key, value映射
    cache map[int]*DLinkedNode
    // 双向链表的头节点和尾节点
    head, tail *DLinkedNode
}

// 双向链表的节点
type DLinkedNode struct {
    // 当前节点的key, value数据
    key, value int
    // 当前节点的上一个节点和下一个节点
    prev, next *DLinkedNode
}

// 初始化双向链表的节点
func initDLinkedNode(key, value int) *DLinkedNode {
    return &DLinkedNode{
        key: key,
        value: value,
    }
}

func Constructor(capacity int) LRUCache {
    // 初始化LRUCache
    l := LRUCache{
        cache: map[int]*DLinkedNode{},
        head: initDLinkedNode(0, 0),
        tail: initDLinkedNode(0, 0),
        capacity: capacity,
    }
    l.head.next = l.tail
    l.tail.prev = l.head
    return l
}

func (this *LRUCache) Get(key int) int {
    // 数据不存在返回-1
    if _, ok := this.cache[key]; !ok {
        return -1
    }
    // 查找哈希表,返回当前节点
    node := this.cache[key]
    // 将该节点移动至头部(及更新节点操作的时间顺序)
    this.moveToHead(node)
    return node.value
}


func (this *LRUCache) Put(key int, value int)  {
    if _, ok := this.cache[key]; !ok {
        // 当前添加的节点不存在

        node := initDLinkedNode(key, value)
        // 加入哈希表
        this.cache[key] = node
        // 添加节点至双向链表头部(及更新当前添加的节点操作的时间顺序)
        this.addToHead(node)
        // 更新缓存的大小
        this.size++
        // 如果缓存的大小大于缓存的容量
        if this.size > this.capacity {
            // 删除当前节点
            removed := this.removeTail()
            delete(this.cache, removed.key)
            // 更新缓存的大小
            this.size--
        }
    } else {
        // 当前添加的节点存在

        // 查找哈希表,返回当前节点
        node := this.cache[key]
        // 更新当前节点的数据
        node.value = value
        // 将该节点移动至头部(及更新节点操作的时间顺序)
        this.moveToHead(node)
    }
}

// 将当前节点加入双向链表的头部
func (this *LRUCache) addToHead(node *DLinkedNode) {
    node.prev = this.head
    node.next = this.head.next
    this.head.next.prev = node
    this.head.next = node
}

// 删除当前节点
func (this *LRUCache) removeNode(node *DLinkedNode) {
    node.prev.next = node.next
    node.next.prev = node.prev
}

// 将当前节点移动至双向链表的头部
func (this *LRUCache) moveToHead(node *DLinkedNode) {
    this.removeNode(node)
    this.addToHead(node)
}

// 删除双向链表的尾部
func (this *LRUCache) removeTail() *DLinkedNode {
    node := this.tail.prev
    this.removeNode(node)
    return node
}
相关推荐
penguin_bark6 分钟前
69. x 的平方根
算法
一休哥助手15 分钟前
Redis 五种数据类型及底层数据结构详解
数据结构·数据库·redis
这可就有点麻烦了16 分钟前
强化学习笔记之【TD3算法】
linux·笔记·算法·机器学习
苏宸啊22 分钟前
顺序表及其代码实现
数据结构·算法
lin zaixi()25 分钟前
贪心思想之——最大子段和问题
数据结构·算法
FindYou.25 分钟前
C - Separated Lunch
算法·深度优先
夜雨翦春韭31 分钟前
【代码随想录Day30】贪心算法Part04
java·数据结构·算法·leetcode·贪心算法
Kent_J_Truman43 分钟前
【平方差 / C】
算法
一直学习永不止步43 分钟前
LeetCode题练习与总结:H 指数--274
java·数据结构·算法·leetcode·数组·排序·计数排序
邵泽明44 分钟前
面试知识储备-多线程
java·面试·职场和发展