|
| 1 | +--- |
| 2 | +title: 146.LRU cache |
| 3 | +date: '2024.01.01 0:00' |
| 4 | +tags: |
| 5 | + - Python |
| 6 | + - answer |
| 7 | + - Linked |
| 8 | + - design |
| 9 | + - Hash table |
| 10 | + - 双向Linked |
| 11 | + |
| 12 | +abbrlink: b9130c0e |
| 13 | +--- |
| 14 | + |
| 15 | +# topic: |
| 16 | +I met for the first time:'2023/3/14-15:21' |
| 17 | + |
| 18 | + |
| 19 | +[146.LRU cache.md](https://leetcode-cn.com/problems/lru-cache/) |
| 20 | + |
| 21 | +# Thought: |
| 22 | +Simple simulation,Use the dictionary to make it,好像完全没用到双向Linked和LRU的Thought。。。I'm guilty,I can remember the air conditioner made in half a year ago。 |
| 23 | +answer就看Code的注释应该就能看懂,I will reply if I don’t understand。 |
| 24 | + |
| 25 | +# Code: |
| 26 | +```python |
| 27 | +class LRUCache: |
| 28 | + |
| 29 | + def __init__(self, capacity: int): |
| 30 | + self.capacity = capacity |
| 31 | + self.cache = collections.OrderedDict() |
| 32 | + |
| 33 | + def get(self, key: int) -> int: |
| 34 | + # ifkeyIn the dictionary,returnvalue |
| 35 | + if key in self.cache: |
| 36 | + self.cache.move_to_end(key) |
| 37 | + return self.cache[key] |
| 38 | + |
| 39 | + else: |
| 40 | + # ifkey不In the dictionary,return-1 |
| 41 | + return -1 |
| 42 | + |
| 43 | + def put(self, key: int, value: int) -> None: |
| 44 | + if key in self.cache: |
| 45 | + # ifkeyIn the dictionary,renewvalue |
| 46 | + self.cache.move_to_end(key) |
| 47 | + self.cache[key] = value |
| 48 | + else: |
| 49 | + if len(self.cache) == self.capacity: |
| 50 | + # ifkey不In the dictionary,The dictionary is full,Delete the least recently used elements |
| 51 | + self.cache.popitem(last=False) |
| 52 | + # ifkey不In the dictionary,Dictionary is not full,Add element |
| 53 | + # self.cache.move_to_end(key) |
| 54 | + self.cache[key] = value |
| 55 | +``` |
| 56 | + |
| 57 | +# Second solution |
| 58 | + |
| 59 | +```python |
| 60 | +class Node: |
| 61 | + # Improve the speed of access attributes,And save memory |
| 62 | + __slots__ = 'prev', 'next', 'key', 'value' |
| 63 | + |
| 64 | + def __init__(self, key=0, value=0): |
| 65 | + self.key = key |
| 66 | + self.value = value |
| 67 | + |
| 68 | +class LRUCache: |
| 69 | + def __init__(self, capacity: int): |
| 70 | + self.capacity = capacity |
| 71 | + self.dummy = Node() # Sentry node |
| 72 | + self.dummy.prev = self.dummy |
| 73 | + self.dummy.next = self.dummy |
| 74 | + self.key_to_node = dict() |
| 75 | + |
| 76 | + def get_node(self, key: int) -> Optional[Node]: |
| 77 | + if key not in self.key_to_node: # No this book |
| 78 | + return None |
| 79 | + node = self.key_to_node[key] # Have this book |
| 80 | + self.remove(node) # Draw this book |
| 81 | + self.push_front(node) # Put on the top |
| 82 | + return node |
| 83 | + |
| 84 | + def get(self, key: int) -> int: |
| 85 | + node = self.get_node(key) |
| 86 | + return node.value if node else -1 |
| 87 | + |
| 88 | + def put(self, key: int, value: int) -> None: |
| 89 | + node = self.get_node(key) |
| 90 | + if node: # Have this book |
| 91 | + node.value = value # renew value |
| 92 | + return |
| 93 | + self.key_to_node[key] = node = Node(key, value) # new book |
| 94 | + self.push_front(node) # Put on the top |
| 95 | + if len(self.key_to_node) > self.capacity: # There are too many books |
| 96 | + back_node = self.dummy.prev |
| 97 | + del self.key_to_node[back_node.key] |
| 98 | + self.remove(back_node) # Remove the last book |
| 99 | + |
| 100 | + # Delete a node(Draw a book) |
| 101 | + def remove(self, x: Node) -> None: |
| 102 | + x.prev.next = x.next |
| 103 | + x.next.prev = x.prev |
| 104 | + |
| 105 | + # 在Linked头添加一个节点(把一本书Put on the top) |
| 106 | + def push_front(self, x: Node) -> None: |
| 107 | + x.prev = self.dummy |
| 108 | + x.next = self.dummy.next |
| 109 | + x.prev.next = x |
| 110 | + x.next.prev = x |
| 111 | +``` |
0 commit comments