🚀 力扣热题 146:LRU 缓存机制(超详细讲解)
📌 题目描述
力扣 146. LRU 缓存
请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。
实现
LRUCache类:
LRUCache(int capacity)以 正整数 作为容量capacity初始化 LRU 缓存。int get(int key)如果关键字key存在于缓存中,则返回关键字的值,否则返回-1。void put(int key, int value)如果关键字已经存在,则变更其数据值;如果不存在,则插入该组键值对。当缓存容量达到上限时,它应该在插入新项之前,使最久未使用的键值对作废。
🎯 示例
            
            
              text
              
              
            
          
          输入:
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2],        [1,1], [2,2], [1],   [3,3], [2],   [4,4], [1],   [3],   [4]]
输出:
[null,       null,  null,   1,    null,  -1,    null,  -1,     3,     4]
        💡 解题思路
✅ 要求实现一个支持 O(1) 时间复杂度的缓存机制:
get(key)和put(key, value)都必须在常数时间完成。- 关键是:快速访问 + 快速淘汰最近最少使用的数据。
 
⛓️ 数据结构设计
我们需要两个结构配合使用:
| 结构 | 作用 | 
|---|---|
| 哈希表(Map) | 实现 O(1) 时间内查找键值对 | 
| 双向链表(Doubly Linked List) | 实现 O(1) 时间内插入和删除节点,用于记录访问顺序 | 
🔨 核心操作
put():- 键已存在:更新值,并将节点移到链表头部。
 - 键不存在:
- 空间足够:新节点插入头部。
 - 空间不足:移除链表尾部节点(最久未使用),插入新节点。
 
 
get():- 键不存在:返回 
-1 - 键存在:返回对应值,并将节点移到链表头部(最近使用)
 
- 键不存在:返回 
 
💻 Go 语言实现代码
            
            
              go
              
              
            
          
          type Node struct {
    key, value int
    prev, next *Node
}
type LRUCache struct {
    capacity int
    cache    map[int]*Node
    head     *Node // 虚拟头
    tail     *Node // 虚拟尾
}
func Constructor(capacity int) LRUCache {
    head := &Node{}
    tail := &Node{}
    head.next = tail
    tail.prev = head
    return LRUCache{
        capacity: capacity,
        cache:    make(map[int]*Node),
        head:     head,
        tail:     tail,
    }
}
func (this *LRUCache) Get(key int) int {
    if node, ok := this.cache[key]; ok {
        this.moveToHead(node)
        return node.value
    }
    return -1
}
func (this *LRUCache) Put(key int, value int) {
    if node, ok := this.cache[key]; ok {
        node.value = value
        this.moveToHead(node)
    } else {
        if len(this.cache) >= this.capacity {
            removed := this.removeTail()
            delete(this.cache, removed.key)
        }
        newNode := &Node{key: key, value: value}
        this.cache[key] = newNode
        this.addToHead(newNode)
    }
}
// 将节点移动到头部
func (this *LRUCache) moveToHead(node *Node) {
    this.removeNode(node)
    this.addToHead(node)
}
// 从链表中移除节点
func (this *LRUCache) removeNode(node *Node) {
    prev := node.prev
    next := node.next
    prev.next = next
    next.prev = prev
}
// 添加节点到头部
func (this *LRUCache) addToHead(node *Node) {
    node.prev = this.head
    node.next = this.head.next
    this.head.next.prev = node
    this.head.next = node
}
// 移除尾部节点(返回被移除的节点)
func (this *LRUCache) removeTail() *Node {
    node := this.tail.prev
    this.removeNode(node)
    return node
}
        ⏳ 复杂度分析
| 操作 | 时间复杂度 | 空间复杂度 | 
|---|---|---|
get() | 
O(1) | 
O(n) | 
put() | 
O(1) | 
O(n) | 
- 所有操作都只涉及 Map 和链表的插入、删除,时间复杂度为常数。
 - 空间复杂度与缓存容量 
n成正比。 
🔍 思维拓展
- 实现 LRU 机制是面试高频考点,特别是系统设计中常用于:
- 数据缓存
 - 页面置换
 - 网络缓存策略
 
 - LRU 和 LFU(最不常用)是最常见的缓存淘汰策略。
 
🎯 总结
| 点评 | 内容 | 
|---|---|
| ✅ 特点 | 常数时间插入、删除、查询 | 
| ✅ 关键 | Map + 双向链表 | 
| ✅ 易错点 | 双向链表指针操作需谨慎 | 
| ✅ 面试高频 | 大厂常考,Redis、浏览器缓存中有应用 | 
如果你觉得这篇文章对你有帮助,欢迎 点赞 👍 收藏 ⭐ 评论 💬 关注 💻,我会持续更新更多 Leetcode 热题解析!📌🎯🚀