Fork me on GitHub

【LeetCode】 146.LUR缓存机制

题目描述

题目难度:★★★★

这是一道LeetCode上的算法题,
题目地址:LRUCache

运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。

获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。
写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。

进阶:

你是否可以在 O(1) 时间复杂度内完成这两种操作?

题目思路

思路也是在网上看了一些大佬的思路才整理出来的现在思路,如有错误地方,请指出。

整体的设计思路是,可以使用 HashMap 存储 key,这样可以做到 save 和 get key的时间都是 O(1),而 HashMap 的 Value 指向双向链表实现的 LRU 的 Node 节点,如图所示(图片是我看网上解题思路的时候顺手就拿下来的)。
img

Map
LRU 存储是基于双向链表实现的,下面的图演示了它的原理。其中 head 代表双向链表的表头,tail 代表尾部。首先预先设置 LRU 的容量,如果存储满了,可以通过 O(1) 的时间淘汰掉双向链表的尾部,每次新增和访问数据,都可以通过 O(1)的效率把新的节点增加到对头,或者把已经存在的节点移动到队头。

下面展示了,预设大小是 3 的,LRU存储的在存储和访问过程中的变化。为了简化图复杂度,图中没有展示 HashMap部分的变化,仅仅演示了上图 LRU 双向链表的变化。我们对这个LRU缓存的操作序列如下:

save("key1", 7)
save("key2", 0)
save("key3", 1)
save("key4", 2)
get("key2")
save("key5", 3)
get("key2")
save("key6", 4)

相应的 LRU 双向链表部分变化如下:
img

实现代码

Java实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
class DLinkedNode {
int key;
int value;
DLinkedNode pre;
DLinkedNode post;
}
public class LRUCache{

/**
* 总是在头节点中插入新节点.
*/
private void addNode(DLinkedNode node) {

node.pre = head;
node.post = head.post;

head.post.pre = node;
head.post = node;
}

/**
* 摘除一个节点.
*/
private void removeNode(DLinkedNode node) {
DLinkedNode pre = node.pre;
DLinkedNode post = node.post;

pre.post = post;
post.pre = pre;
}

/**
* 摘除一个节点,并且将它移动到开头
*/
private void moveToHead(DLinkedNode node) {
this.removeNode(node);
this.addNode(node);
}

/**
* 弹出最尾巴节点
*/
private DLinkedNode popTail() {
DLinkedNode res = tail.pre;
this.removeNode(res);
return res;
}

private HashMap<Integer, DLinkedNode>
cache = new HashMap<Integer, DLinkedNode>();
private int count;
private int capacity;
private DLinkedNode head, tail;

public LRUCache(int capacity) {
this.count = 0;
this.capacity = capacity;

head = new DLinkedNode();
head.pre = null;

tail = new DLinkedNode();
tail.post = null;

head.post = tail;
tail.pre = head;
}

public int get(int key) {

DLinkedNode node = cache.get(key);
if (node == null) {
return -1; // cache里面没有
}

// cache 命中,挪到开头
this.moveToHead(node);

return node.value;
}


public void put(int key, int value) {
DLinkedNode node = cache.get(key);

if (node == null) {

DLinkedNode newNode = new DLinkedNode();
newNode.key = key;
newNode.value = value;

this.cache.put(key, newNode);
this.addNode(newNode);

++count;

if (count > capacity) {
// 最后一个节点弹出
DLinkedNode tail = this.popTail();
this.cache.remove(tail.key);
count--;
}
} else {
// cache命中,更新cache.
node.value = value;
this.moveToHead(node);
}
}
}

C++实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class LRUCache {
public:
LRUCache(int capacity) {
cap = capacity;
}

int get(int key) {
auto it = map.find(key);
if (it == map.end())
return -1;
ll.splice(ll.begin(), ll, it->second);
return it->second->second;
}

void put(int key, int value) {
auto it = map.find(key);
if (it != map.end())
ll.erase(it->second);
ll.push_front(make_pair(key, value));
map[key] = ll.begin();
if (map.size() > cap) {
int k = ll.rbegin()->first;
ll.pop_back();
map.erase(k);
}
}

private:
int cap;
list<pair<int, int>> ll;
unordered_map<int, list<pair<int, int> >::iterator> map;
};

核心总结:

save(key, value),首先在 HashMap 找到 Key 对应的节点,如果节点存在,更新节点的值,并把这个节点移动队头。如果不存在,需要构造新的节点,并且尝试把节点塞到队头,如果LRU空间不足,则通过 tail 淘汰掉队尾的节点,同时在 HashMap 中移除 Key。

get(key),通过 HashMap 找到 LRU 链表节点,因为根据LRU 原理,这个节点是最新访问的,所以要把节点插入到队头,然后返回缓存的值。

本文标题:【LeetCode】 146.LUR缓存机制

文章作者:LiuXiaoKun

发布时间:2019年06月21日 - 13:06

最后更新:2019年06月21日 - 16:06

原始链接:https://LiuZiQiao.github.io/2019/06/21/【LeetCode】146.LUR缓存机制/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%