Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 90fe2d3

Browse files
Create 146. LRU 缓存.md
1 parent 8d9f125 commit 90fe2d3

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

‎Hash Table/146. LRU 缓存.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#### 146. LRU 缓存
2+
3+
难度:中等
4+
5+
---
6+
7+
请你设计并实现一个满足 [LRU (最近最少使用) 缓存](https://baike.baidu.com/item/LRU) 约束的数据结构。
8+
9+
实现 `LRUCache` 类:
10+
11+
* `LRUCache(int capacity)`**正整数** 作为容量 `capacity` 初始化 LRU 缓存
12+
* `int get(int key)` 如果关键字 `key` 存在于缓存中,则返回关键字的值,否则返回 `-1`
13+
* `void put(int key, int value)` 如果关键字 `key` 已经存在,则变更其数据值 `value` ;如果不存在,则向缓存中插入该组 `key-value` 。如果插入操作导致关键字数量超过 `capacity` ,则应该 **逐出** 最久未使用的关键字。
14+
15+
函数 `get``put` 必须以 `O(1)` 的平均时间复杂度运行。
16+
17+
**示例:**
18+
19+
```
20+
输入
21+
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
22+
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
23+
输出
24+
[null, null, null, 1, null, -1, null, -1, 3, 4]
25+
26+
解释
27+
LRUCache lRUCache = new LRUCache(2);
28+
lRUCache.put(1, 1); // 缓存是 {1=1}
29+
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
30+
lRUCache.get(1); // 返回 1
31+
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
32+
lRUCache.get(2); // 返回 -1 (未找到)
33+
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
34+
lRUCache.get(1); // 返回 -1 (未找到)
35+
lRUCache.get(3); // 返回 3
36+
lRUCache.get(4); // 返回 4
37+
```
38+
39+
**提示:**
40+
41+
* `1 <= capacity <= 3000`
42+
* `0 <= key <= 10000`
43+
* `0 <= value <= 10^5`
44+
* 最多调用 `2 * 10^5``get``put`
45+
46+
---
47+
48+
哈希表 + 双向链表:
49+
50+
![](https://pic.leetcode.cn/1696039105-PSyHej-146-3-c.png)
51+
52+
```Java
53+
type LRUCache struct {
54+
doubleLinkedList *list.List
55+
hashmap map[int]*list.Element
56+
capacity int
57+
}
58+
59+
type entry struct {
60+
k, v int
61+
}
62+
63+
func Constructor(capacity int) LRUCache {
64+
return LRUCache{list.New(), make(map[int]*list.Element), capacity}
65+
}
66+
67+
func (this *LRUCache) Get(key int) int {
68+
node := this.hashmap[key]
69+
if node == nil {
70+
return -1
71+
}
72+
this.doubleLinkedList.MoveToFront(node)
73+
return node.Value.(entry).v
74+
}
75+
76+
func (this *LRUCache) Put(key int, value int) {
77+
node := this.hashmap[key]
78+
if node != nil {
79+
node.Value = entry{key, value}
80+
this.doubleLinkedList.MoveToFront(node)
81+
return
82+
}
83+
this.hashmap[key] = this.doubleLinkedList.PushFront(entry{key, value})
84+
if this.capacity < len(this.hashmap) {
85+
delete(this.hashmap, this.doubleLinkedList.Remove(this.doubleLinkedList.Back()).(entry).k)
86+
}
87+
}
88+
89+
90+
/**
91+
* Your LRUCache object will be instantiated and called as such:
92+
* obj := Constructor(capacity);
93+
* param_1 := obj.Get(key);
94+
* obj.Put(key,value);
95+
*/
96+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /