【LeetCode】LRU缓存

目录


一、题目

请你设计并实现一个满足 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

提示:

1 <= capacity <= 3000

0 <= key <= 10000

0 <= value <= 105

最多调用 2 * 105 次 get 和 put

二、解法

用双向链表,为什么是双向的呢,因为lru更新的时候,我们要把它插入到最新的位置,所以在只知道一个位置的时候,双向链表插入的时候,可以做到O(1),而单链表需要先找到指向这个位置的指针,时间复杂度就变为O(n)了。因为get和put的时间复杂度要求O(1)
getput穿的参数都有key,可以让key为键,node为值来进行操作。

做这种题需要有耐心,而且需要细心,对于我这种很粗心的人来说,可能要多做做这种设计的题,来锻炼一下自己


完整代码

python 复制代码
class Node:
    def __init__(self, key = 0, value = 0):
        self.prev = None
        self.next = None
        self.key = key
        self.value = value

class LRUCache:

    def __init__(self, capacity: int):
        self.capacity = capacity
        self.head = Node()
        self.tail = Node()
        self.head.next = self.tail
        self.tail.prev = self.head
        self.dic = {}

    def move_to_tail(self, key):
        node = self.dic.get(key)
        # 自己先断开
        node.prev.next = node.next
        node.next.prev = node.prev
        node.prev = self.tail.prev
        node.next = self.tail
        self.tail.prev.next = node
        self.tail.prev = node

    def get(self, key: int) -> int:
        if key not in self.dic:
            return -1
        self.move_to_tail(key)
        node = self.dic.get(key)
        return node.value

    def put(self, key: int, value: int) -> None:
        # 已经存在,则更新
        if key in self.dic:
            self.dic[key].value = 
            self.move_to_tail(key)
        else:
            # 满了,删除头部的(最老的)
            if len(self.dic) == self.capacity:
                old_node = self.head.next
                old_key = old_node.key
                self.dic.pop(old_key)
                old_node.prev.next = old_node.next
                old_node.next.prev = old_node.prev
            new_node = Node(key, value)
            self.dic[key] = new_node
            new_node.next = self.tail
            new_node.prev = self.tail.prev
            self.tail.prev.next = new_node
            self.tail.prev = new_node


# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)
```
相关推荐
计信金边罗1 小时前
是否存在路径(FIFOBB算法)
算法·蓝桥杯·图论
MZWeiei1 小时前
KMP 算法中 next 数组的构建函数 get_next
算法·kmp
Fanxt_Ja2 小时前
【JVM】三色标记法原理
java·开发语言·jvm·算法
luofeiju3 小时前
行列式的性质
线性代数·算法·矩阵
緈福的街口3 小时前
【leetcode】347. 前k个高频元素
算法·leetcode·职场和发展
半桔3 小时前
【Linux手册】冯诺依曼体系结构
linux·缓存·职场和发展·系统架构
-qOVOp-3 小时前
408第一季 - 408内容概述
数据结构
pen-ai3 小时前
【统计方法】基础分类器: logistic, knn, svm, lda
算法·机器学习·支持向量机
鑫鑫向栄3 小时前
[蓝桥杯]春晚魔术【算法赛】
算法·职场和发展·蓝桥杯
roman_日积跬步-终至千里4 小时前
【Go语言基础【3】】变量、常量、值类型与引用类型
开发语言·算法·golang