leetCode 146. LRU 缓存

146. 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

    class LinkList:
    def init(self,key,value):
    self.key=key
    self.value=value
    self.next=None
    self.prev=None

    class LRUCache:

    复制代码
      def __init__(self, capacity: int):
          self.d={}
          self.capacity=capacity
          self.head=LinkList(0,0)
          self.tail=LinkList(0,0)
          self.head.next=self.tail
          self.tail.prev=self.head
    
    
      def add(self,node):
          self.tail.prev.next=node
          node.prev=self.tail.prev
          node.next=self.tail
          self.tail.prev=node
          
      
      def remove(self,node):
          node.prev.next=node.next
          node.next.prev=node.prev
          
    
      def update(self,node):
          self.remove(node)
          self.add(node)
          
          
      def get(self, key: int) -> int:
          if key in self.d:
              self.update(self.d[key])
              # print(self.head.next.value)
              # print(self.tail.prev.value)
              return self.d[key].value
          else:
              # print(self.head.next.value)
              # print(self.tail.prev.value)
              return -1
    
    
      def put(self, key: int, value: int) -> None:
          if key in self.d:
              self.d[key].value=value
              self.update(self.d[key])
          else:
              node=LinkList(key,value)
              self.d[key]=node
              self.add(self.d[key])
          
          if len(self.d)>self.capacity:
              self.d.pop(self.head.next.key)
              self.remove(self.head.next)
          # print(self.head.next.value)
          # print(self.tail.prev.value)

    Your LRUCache object will be instantiated and called as such:

    obj = LRUCache(capacity)

    param_1 = obj.get(key)

    obj.put(key,value)

相关推荐
黑客笔记8 小时前
python小工具win7翻车事故现场
python
岁月宁静10 小时前
从零拆解 AI 语音模拟面试系统
人工智能·python·agent
benchmark_cc11 小时前
个人开发者接入五档盘口:用 QuantDash 读取单只股票的验收清单与易错点
python·数据分析·pandas·量化交易·股票数据·quantdash
ZhengEnCi12 小时前
项目越写越乱?先别改架构:用一次目录重排 + 一份可复算的体检脚本把乱治住
python·架构·ai编程
Dmz丶13 小时前
前端转 AI 100 天 Day 45:阶段复盘——从“会调 API”到“做出 AI 产品”
python
Sakura_j_h_13 小时前
MCP SDK 2.0 实战:用 MCPServer 搭本地笔记检索服务
开发语言·笔记·python·qt·ai工具·mcp·mcpserver
QuZhengRong14 小时前
【AI】Agent 全栈进阶|AgentScope 沙箱源码解析
python·学习·ai·agent·沙箱
Eider199814 小时前
Python基础语法之并发相关3
开发语言·python
M--Y15 小时前
LangGraph 架构基石:持久化与运行控制
python·langchain·大模型·llm·持久化·langgraph
用户0190275816116 小时前
新浪财经 / 东方财富股票接口不稳、被限?稳定替代方案
python