|
| 1 | +# [146. LRU Cache](https://leetcode.com/problems/lru-cache/) |
| 2 | + |
| 3 | +## 题目 |
| 4 | + |
| 5 | +Design a data structure that follows the constraints of a **[Least Recently Used (LRU) cache](https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU)**. |
| 6 | + |
| 7 | +Implement the `LRUCache` class: |
| 8 | + |
| 9 | +- `LRUCache(int capacity)` Initialize the LRU cache with **positive** size `capacity`. |
| 10 | +- `int get(int key)` Return the value of the `key` if the key exists, otherwise return `1`. |
| 11 | +- `void put(int key, int value)` Update the value of the `key` if the `key` exists. Otherwise, add the `key-value` pair to the cache. If the number of keys exceeds the `capacity` from this operation, **evict** the least recently used key. |
| 12 | + |
| 13 | +**Follow up:**Could you do `get` and `put` in `O(1)` time complexity? |
| 14 | + |
| 15 | +**Example 1:** |
| 16 | + |
| 17 | +``` |
| 18 | +Input |
| 19 | +["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] |
| 20 | +[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] |
| 21 | +Output |
| 22 | +[null, null, null, 1, null, -1, null, -1, 3, 4] |
| 23 | +
|
| 24 | +Explanation |
| 25 | +LRUCache lRUCache = new LRUCache(2); |
| 26 | +lRUCache.put(1, 1); // cache is {1=1} |
| 27 | +lRUCache.put(2, 2); // cache is {1=1, 2=2} |
| 28 | +lRUCache.get(1); // return 1 |
| 29 | +lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3} |
| 30 | +lRUCache.get(2); // returns -1 (not found) |
| 31 | +lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3} |
| 32 | +lRUCache.get(1); // return -1 (not found) |
| 33 | +lRUCache.get(3); // return 3 |
| 34 | +lRUCache.get(4); // return 4 |
| 35 | +
|
| 36 | +``` |
| 37 | + |
| 38 | +**Constraints:** |
| 39 | + |
| 40 | +- `1 <= capacity <= 3000` |
| 41 | +- `0 <= key <= 3000` |
| 42 | +- `0 <= value <= 104` |
| 43 | +- At most `3 * 104` calls will be made to `get` and `put`. |
| 44 | + |
| 45 | +## 题目大意 |
| 46 | + |
| 47 | +运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制 。 |
| 48 | +实现 LRUCache 类: |
| 49 | + |
| 50 | +- LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存 |
| 51 | +- int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。 |
| 52 | +- void put(int key, int value) 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。 |
| 53 | + |
| 54 | +进阶:你是否可以在 O(1) 时间复杂度内完成这两种操作? |
| 55 | + |
| 56 | +## 解题思路 |
| 57 | + |
| 58 | +- 这一题是 LRU 经典面试题,详细解释见第三章模板。 |
| 59 | + |
| 60 | +## 代码 |
| 61 | + |
| 62 | +```go |
| 63 | +package leetcode |
| 64 | + |
| 65 | +type LRUCache struct { |
| 66 | + head, tail *Node |
| 67 | + Keys map[int]*Node |
| 68 | + Cap int |
| 69 | +} |
| 70 | + |
| 71 | +type Node struct { |
| 72 | + Key, Val int |
| 73 | + Prev, Next *Node |
| 74 | +} |
| 75 | + |
| 76 | +func Constructor(capacity int) LRUCache { |
| 77 | + return LRUCache{Keys: make(map[int]*Node), Cap: capacity} |
| 78 | +} |
| 79 | + |
| 80 | +func (this *LRUCache) Get(key int) int { |
| 81 | + if node, ok := this.Keys[key]; ok { |
| 82 | + this.Remove(node) |
| 83 | + this.Add(node) |
| 84 | + return node.Val |
| 85 | + } |
| 86 | + return -1 |
| 87 | +} |
| 88 | + |
| 89 | +func (this *LRUCache) Put(key int, value int) { |
| 90 | + if node, ok := this.Keys[key]; ok { |
| 91 | + node.Val = value |
| 92 | + this.Remove(node) |
| 93 | + this.Add(node) |
| 94 | + return |
| 95 | + } else { |
| 96 | + node = &Node{Key: key, Val: value} |
| 97 | + this.Keys[key] = node |
| 98 | + this.Add(node) |
| 99 | + } |
| 100 | + if len(this.Keys) > this.Cap { |
| 101 | + delete(this.Keys, this.tail.Key) |
| 102 | + this.Remove(this.tail) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func (this *LRUCache) Add(node *Node) { |
| 107 | + node.Prev = nil |
| 108 | + node.Next = this.head |
| 109 | + if this.head != nil { |
| 110 | + this.head.Prev = node |
| 111 | + } |
| 112 | + this.head = node |
| 113 | + if this.tail == nil { |
| 114 | + this.tail = node |
| 115 | + this.tail.Next = nil |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func (this *LRUCache) Remove(node *Node) { |
| 120 | + if node == this.head { |
| 121 | + this.head = node.Next |
| 122 | + node.Next = nil |
| 123 | + return |
| 124 | + } |
| 125 | + if node == this.tail { |
| 126 | + this.tail = node.Prev |
| 127 | + node.Prev.Next = nil |
| 128 | + node.Prev = nil |
| 129 | + return |
| 130 | + } |
| 131 | + node.Prev.Next = node.Next |
| 132 | + node.Next.Prev = node.Prev |
| 133 | +} |
| 134 | +``` |
0 commit comments