146. LRU 缓存

题目地址(146. LRU 缓存)

https://leetcode.cn/problems/lru-cache/

题目描述

请你设计并实现一个满足  LRU (最近最少使用) 缓存 约束的数据结构。
实现 LRUCache 类:
LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存
int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
void put(int key, int value) 如果关键字 key 已经存在,则变更其数据值 value ;如果不存在,则向缓存中插入该组 key-value 。如果插入操作导致关键字数量超过 capacity ,则应该 逐出 最久未使用的关键字。

函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。

 

示例:

输入
["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]

解释
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // 缓存是 {1=1}
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
lRUCache.get(1);    // 返回 1
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
lRUCache.get(2);    // 返回 -1 (未找到)
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
lRUCache.get(1);    // 返回 -1 (未找到)
lRUCache.get(3);    // 返回 3
lRUCache.get(4);    // 返回 4

前置知识

公司

  • 暂无

思路

  • 该题,核心理解缓存的基本操作即可
  • Get时,将改点移动到头节点位置(涉及到从原来位置删除,添加到头节点处,两个操作)
  • Put时,如果存在,则更新,并移动到头节点;如果不存在,则新建节点,加入到头节点处,并且如果size > capacity,还需要移除末尾处节点
  • 另外,注意Go语言删除map,是delete(map, key)
  • 另外,为了简化链表操作,可以在首尾使用dummy哑结点简化链表操作

关键点

代码

  • 语言支持:Go

Go Code:


type LRUCache struct {
    size, capacity int
    cache map[int]*DListNode
    head, tail *DListNode
}

type DListNode struct {
    key, value int
    prev, next *DListNode
}

func  initListNode(key, value int) *DListNode {
    return &DListNode{
        key: key,
        value: value,
    }
}


func Constructor(capacity int) LRUCache {
    list := LRUCache {
        capacity: capacity,
        cache: map[int]*DListNode{},
        head: initListNode(0, 0),
        tail: initListNode(0, 0),
    }
    list.head.next = list.tail
    list.tail.next = list.head
    return list
}

func (this *LRUCache) moveToHead(node *DListNode) {
    // 去除node前后级关系
    node.prev.next = node.next
    node.next.prev = node.prev
    next := this.head.next
    this.head.next = node
    node.next = next
    next.prev = node
    node.prev = this.head
}


func (this *LRUCache) Get(key int) int {
    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 := initListNode(key, value)
        this.cache[key] = node
        tmp := this.head.next
        this.head.next = node
        node.next = tmp
        tmp.prev = node
        node.prev = this.head
        // 如果size超过了capacity,则需要删除最老的
        this.size++
        if this.size > this.capacity {
            last := this.tail.prev
            last.prev.next = last.next
            last.next.prev = last.prev
            // map里面的东需要删除
            delete(this.cache, last.key)
            this.size--
        }
    } else {
        node := this.cache[key]
        node.value = value
        this.moveToHead(node)
    }
}

/**
 * Your LRUCache object will be instantiated and called as such:
 * obj := Constructor(capacity);
 * param_1 := obj.Get(key);
 * obj.Put(key,value);
 */

更规范、更简洁的做法

type LRUCache struct {
    size, capacity int
    cache map[int]*DListNode
    head, tail *DListNode
}

type DListNode struct {
    key, value int
    prev, next *DListNode
}

func  initListNode(key, value int) *DListNode {
    return &DListNode{
        key: key,
        value: value,
    }
}


func Constructor(capacity int) LRUCache {
    list := LRUCache {
        capacity: capacity,
        cache: map[int]*DListNode{},
        head: initListNode(0, 0),
        tail: initListNode(0, 0),
    }
    list.head.next = list.tail
    list.tail.next = list.head
    return list
}

func (this *LRUCache) moveToHead(node *DListNode) {
    // 去除node前后级关系
    node.prev.next = node.next
    node.next.prev = node.prev

}


func (this *LRUCache) Get(key int) int {
    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 := initListNode(key, value)
        this.cache[key] = node
        this.addToHead(node)
        // 如果size超过了capacity,则需要删除最老的
        this.size++
        if this.size > this.capacity {
            last := this.tail.prev
            this.removeNode(last)
            // map里面的东需要删除
            delete(this.cache, last.key)
            this.size--
        }
    } else {
        node := this.cache[key]
        node.value = value
        this.moveToHead(node)
    }
}

func (this *LRUCache) removeNode(node *DListNode) *DListNode{
    node.prev.next = node.next
    node.next.prev = node.prev
    return node
}

func (this *LRUCache) addToHead(node *DListNode) {
    next := this.head.next
    this.head.next = node
    node.next = next
    next.prev = node
    node.prev = this.head
}

/**
 * Your LRUCache object will be instantiated and called as such:
 * obj := Constructor(capacity);
 * param_1 := obj.Get(key);
 * obj.Put(key,value);
 */

复杂度分析

令 n 为数组长度。

  • 时间复杂度:O(n)
  • 空间复杂度:O(1)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容