← 返回题库
中级

LRU缓存机制

未完成
中级参考 代码结构已给出,请填写 ____ 处
from collections import OrderedDict
def solve():
    class LRUCache:
        def __init__(____):
            self.capacity = capacity
            self.cache = OrderedDict()
        def get(____):
            if key not in self.cache:
                return -____
            self.cache.move_to_end(____)
            return self.cache[key]
        def put(____):
            if key in self.cache:
                self.cache.move_to_end(____)
            self.cache[key] = value
            if len(____) > self.capacity:
                self.cache.popitem(____)
    cache = LRUCache(____)
    cache.put(____, ____)
    cache.put(____, ____)
    print(cache.get(____))
    cache.put(____, ____)
    print(cache.get(____))

示例

输入
solve()
期望输出
1
-1
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码