题目:LRU缓存机制
- 题号:146
- 难度:中等
- https://leetcode-cn.com/problems/lru-cache/
请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。
实现 LRUCache 类:
LRUCache(int capacity)
以 正整数 作为容量capacity
初始化 LRU 缓存。int get(int key)
如果关键字key
存在于缓存中,则返回关键字的值,否则返回 -1 。void put(int key, int value)
如果关键字key
已经存在,则变更其数据值value
;如果不存在,则向缓存中插入该组key-value
。如果插入操作导致关键字数量超过capacity
,则应该 逐出 最久未使用的关键字。
函数 get
和 put
必须以 O(1)
的平均时间复杂度运行。
示例:
输入
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
输出
[null, null, null, 1, null, -1, null, -1, 3, 4]
解释
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // 缓存是 {1=1}
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
lRUCache.get(1); // 返回 1
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
lRUCache.get(2); // 返回 -1 (未找到)
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
lRUCache.get(1); // 返回 -1 (未找到)
lRUCache.get(3); // 返回 3
lRUCache.get(4); // 返回 4
提示:
- 1 <= capacity <= 3000
- 0 <= key <= 10000
- 0 < = v a l u e < = 1 0 5 0 <= value <= 10^5 0<=value<=105
最多调用
2
∗
1
0
5
2 * 10^5
2∗105 次 get
和 put
。
实现
计算机的缓存容量有限,如果缓存满了就要删除一些内容,给新内容腾位置。但问题是,删除哪些内容呢?我们肯定希望删掉哪些没什么用的缓存,而把有用的数据继续留在缓存里,方便之后继续使用。那么,什么样的数据,我们判定为「有用的」的数据呢?
LRU 缓存淘汰算法就是一种常用策略。LRU 的全称是 Least Recently Used,也就是说我们认为最近使用过的数据应该是是「有用的」,很久都没用过的数据应该是无用的,内存满了就优先删那些很久没用过的数据。
第一种:利用单链表的方式:
public class LRUCache
{
private readonly int _length;
private readonly List<KeyValuePair<int, int>> _lst;
public LRUCache(int capacity)
{
_length = capacity;
_lst = new List<KeyValuePair<int, int>>();
}
private int GetIndex(int key)
{
for (int i=0,len=_lst.Count;i<len;i++)
{
if (_lst[i].Key == key)
{
return i;
}
}
return -1;
}
public int Get(int key)
{
int index = GetIndex(key);
if (index!=-1)
{
int val = _lst[index].Value;
_lst.RemoveAt(index);
_lst.Add(new KeyValuePair<int, int>(key, val));
return val;
}
return -1;
}
public void Put(int key, int value)
{
int index = GetIndex(key);
if (index!=-1)
{
_lst.RemoveAt(index);
}
else if (_lst.Count == _length)
{
_lst.RemoveAt(0);
}
_lst.Add(new KeyValuePair<int, int>(key, value));
}
}
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.Get(key);
* obj.Put(key,value);
*/
第二种:利用 字典 + 列表 的方式:
把字典当作一个存储容器,由于字典是无序的,即 dict
内部存放的顺序和 key
放入的顺序是没有关系的,所以需要一个 list
来辅助排序。
C# 语言
public class LRUCache
{
private readonly List<int> _keys;
private readonly Dictionary<int, int> _dict;
public LRUCache(int capacity)
{
_keys = new List<int>(capacity);
_dict = new Dictionary<int, int>(capacity);
}
public int Get(int key)
{
if (_dict.ContainsKey(key))
{
_keys.Remove(key);
_keys.Add(key);
return _dict[key];
}
return -1;
}
public void Put(int key, int value)
{
if (_dict.ContainsKey(key))
{
_dict.Remove(key);
_keys.Remove(key);
}
else if (_keys.Count == _keys.Capacity)
{
_dict.Remove(_keys[0]);
_keys.RemoveAt(0);
}
_keys.Add(key);
_dict.Add(key, value);
}
}
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.Get(key);
* obj.Put(key,value);
*/
Python 语言
class LRUCache(object):
def __init__(self, capacity):
"""
:type capacity: int
"""
self._capacity = capacity
self._dict = dict()
self._keys = list()
def get(self, key):
"""
:type key: int
:rtype: int
"""
if key in self._dict:
self._keys.remove(key)
self._keys.append(key)
return self._dict[key]
return -1
def put(self, key, value):
"""
:type key: int
:type value: int
:rtype: None
"""
if key in self._dict:
self._dict.pop(key)
self._keys.remove(key)
elif len(self._keys) == self._capacity:
self._dict.pop(self._keys[0])
self._keys.remove(self._keys[0])
self._keys.append(key)
self._dict[key] = value
# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)