【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为值来进行操作。

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


完整代码

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)
```

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值