此题看题解
python
class ListNode:
def __init__(self, key=None, value=None):
self.key = key
self.value = value
self.prev = None
self.next = None
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.cache = {}
self.head = ListNode()
self.tail = ListNode()
self.head.next = self.tail
self.tail.prev = self.head
def get(self, key: int) -> int:
if key in self.cache:
node = self.cache[key]
self._move_to_head(node)
return node.value
else:
return -1
def put(self, key: int, value: int) -> None:
if key in self.cache:
node = self.cache[key]
node.value = value
self._move_to_head(node)
else:
if len(self.cache) == self.capacity:
self._remove_tail()
node = ListNode(key, value)
self.cache[key] = node
self._add_to_head(node)
def _move_to_head(self, node: ListNode) -> None:
self._remove_node(node)
self._add_to_head(node)
def _remove_node(self, node: ListNode) -> None:
node.prev.next = node.next
node.next.prev = node.prev
def _add_to_head(self, node: ListNode) -> None:
node.prev = self.head
node.next = self.head.next
self.head.next.prev = node
self.head.next = node
def _remove_tail(self) -> None:
node = self.tail.prev
self._remove_node(node)
del self.cache[node.key]
# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)