0
点赞
收藏
分享

微信扫一扫

LeetCode-146. LRU 缓存 -- Python解

小编 2022-04-27 阅读 110

题目描述

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

题解

主要的思路是结合哈希表和链表,将搜索的时间复杂度降到 O(1),链表用的是双向链表

class LinkNode:
    def __init__(self, key, val, pre=None, next=None):
        self.pre = pre
        self.next = next
        self.val = val
        self.key = key


class LRUCache:

    def __init__(self, capacity: int):
        self.capacity = capacity
        self.catch = {}
        self.size = 0
        self.head = LinkNode(-1,0)
        self.tail = LinkNode(-1,0)
        self.head.next = self.tail
        self.tail.pre = self.head

    def get(self, key: int) -> int:
        if key not in self.catch:
            return -1
        self.move_to_top(self.catch.get(key))
        return self.catch.get(key).val

    def put(self, key: int, value: int) -> None:
        if key in self.catch:
            self.catch.get(key).val = value
            self.move_to_top(self.catch.get(key))
        else:
            node = LinkNode(key, value)
            self.catch[key] = node
            self.add_to_top(node)
            self.size += 1
            if self.size > self.capacity:
                remove_node = self.remove_tail()
                del self.catch[remove_node.key]
                self.size -= 1

    def add_to_top(self, node: LinkNode):
        node.pre = self.head
        node.next = self.head.next
        self.head.next.pre = node
        self.head.next = node
        

    def remove_node(self, node: LinkNode)-> LinkNode:
        node.next.pre = node.pre
        node.pre.next = node.next

    def move_to_top(self, node: LinkNode):
        self.remove_node(node)
        self.add_to_top(node)

    def remove_tail(self):
        if self.tail.pre is self.head:
            return 
        true_tail = self.tail.pre
        self.tail.pre = true_tail.pre
        true_tail.pre.next = self.tail
        return true_tail


# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)
举报

相关推荐

LeetCode 146. LRU 缓存

Leetcode.146 LRU 缓存

Leetcode146. LRU 缓存

Leetcode 146. LRU 缓存

LeetCode146. LRU 缓存

【LeetCode】146.LRU缓存

0 条评论