• LeetCode:LRU


    一、OrderedDict

     1 class LRUCache:
     2     def __init__(self, capacity: int):
     3         from collections import OrderedDict
     4         self.cap = capacity
     5         self.dic = OrderedDict()
     6     def get(self, key: int) -> int:
     7         if key not in self.dic:
     8             return -1
     9         self.dic.move_to_end(key)
    10         return self.dic[key]
    11 
    12     def put(self, key: int, value: int) -> None:
    13         if key in self.dic:
    14             self.dic.move_to_end(key)
    15         self.dic[key] = value
    16         if len(self.dic) > self.cap:
    17             self.dic.popitem(last=False)
    18 
    19 # Your LRUCache object will be instantiated and called as such:
    20 # obj = LRUCache(capacity)
    21 # param_1 = obj.get(key)
    22 # obj.put(key,value)

    二、双向链表+dict

     1 class DLinkedNode():
     2     def __init__(self, key=0, value=0):
     3         self.key = key
     4         self.value = value
     5         self.prev = None
     6         self.next = None
     7 
     8 class LRUCache:
     9     def __init__(self, capacity: int):
    10         self.cache = {}
    11         self.size = 0
    12         self.capacity = capacity
    13         self.head, self.tail = DLinkedNode(), DLinkedNode()
    14 
    15         self.head.next = self.tail
    16         self.tail.prev = self.head
    17 
    18     def _add_node(self, node):
    19         node.prev = self.head
    20         node.next = self.head.next
    21 
    22         self.head.next.prev = node
    23         self.head.next = node
    24     
    25     def _remove_node(self, node):
    26         prev = node.prev
    27         new = node.next
    28 
    29         prev.next = new
    30         new.prev = prev
    31 
    32     def _move_to_head(self, node):
    33         self._remove_node(node)
    34         self._add_node(node)
    35 
    36     def _pop_tail(self):
    37         res = self.tail.prev
    38         self._remove_node(res)
    39         return res
    40     
    41     def get(self, key: int) -> int:
    42         node = self.cache.get(key, None)
    43         if not node:
    44             return -1
    45         self._move_to_head(node)
    46         return node.value
    47 
    48     def put(self, key: int, value: int) -> None:
    49         node = self.cache.get(key)
    50         
    51         if not node:
    52             newNode = DLinkedNode(key, value)
    53             
    54             self.cache[key] = newNode
    55             self._add_node(newNode)
    56 
    57             self.size += 1
    58             
    59             if self.size > self.capacity:
    60                 tail = self._pop_tail()
    61                 del self.cache[tail.key]
    62                 self.size -= 1
    63         else:
    64             node.value = value
    65             self._move_to_head(node)
    66 
    67 
    68 
    69 
    70 # Your LRUCache object will be instantiated and called as such:
    71 # obj = LRUCache(capacity)
    72 # param_1 = obj.get(key)
    73 # obj.put(key,value)
  • 相关阅读:
    限制泛型可用类型
    泛型的常规用法(声明两个类型)
    一个类似于金字塔的图形
    Fibonacci数
    快来秒杀我
    奇偶数分离
    Background
    Financial Management
    HangOver
    Binary String Matching
  • 原文地址:https://www.cnblogs.com/liushoudong/p/12570309.html
Copyright © 2020-2023  润新知