LRU缓存结构
题目描述

思路
采用双向链表存储所有键值对,最近操作的键值对在链表头部。为了提高set和get的效率,采用从key映射到对应迭代器的哈希表
核心代码
class Solution {
public:
int cap;
list<pair<int,int>> dlist;
unordered_map<int,list<pair<int,int>>::iterator> hash;
Solution(int capacity){
cap=capacity;
}
int get(int key) {
if(!hash.count(key)) return -1;
//if(hash.find(key)==hash.end()) return -1;
auto temp=*hash[key];
dlist.erase(hash[key]);
dlist.push_front(temp);
hash[key]=dlist.begin();
return dlist.front().second;
}
void set(int key, int value){
if(hash.count(key)){
dlist.erase(hash[key]);
}
else if(cap==dlist.size()){
auto temp=dlist.back();
dlist.pop_back();
hash.erase(temp.first);
}
dlist.push_front(pair<int,int>{key,value});
hash[key]=dlist.begin();
}
};
LFU缓存结构
题目描述

思路
操作次数相同的节点放在一条链表中,最近操作的节点插入在头端,每个节点为一个vector,其中依次存有操作次数,键,值。
增加一个由操作频率映射到双向链表的哈希表,并采用全局变量min_freq存当前最新操作次数,size存剩余容量。
核心代码
class Solution {
public:
/**
* lfu design
* @param operators int整型vector<vector<>> ops
* @param k int整型 the k
* @return int整型vector
*/
unordered_map<int,list<vector<int>>> freq_mp;
unordered_map<int,list<vector<int>>::iterator> mp;
int size=0;
int min_freq=0;
vector<int> LFU(vector<vector<int> >& operators, int k) {
vector<int> res;
size=k;
int n=operators.size();
for(int i=0;i<n;++i){
if(operators[i][0]==1){
set(operators[i][1],operators[i][2]);
}
else{
res.push_back(get(operators[i][1]));
}
}
return res;
}
int get(int key){
if(mp.find(key)==mp.end()) return -1;
auto iter=mp[key];
int res=(*iter)[2];
update(iter,key,res);
return res;
}
void update(list<vector<int>>::iterator iter,int key,int value){
int freq=(*iter)[0];
freq_mp[freq].erase(iter);
if(freq_mp[freq].empty()){
freq_mp.erase(freq);
if(freq==min_freq) ++min_freq;
}
freq_mp[freq+1].push_front({freq+1,key,value});
mp[key]=freq_mp[freq+1].begin();
}
void set(int key,int value){
if(mp.find(key)!=mp.end()){
update(mp[key],key,value);
return;
}
if(!size){
int del_key=freq_mp[min_freq].back()[1];
freq_mp[min_freq].pop_back();
if(freq_mp[min_freq].empty()){
freq_mp.erase(min_freq);
}
mp.erase(del_key);
}
else{
--size;
}
min_freq=1;
freq_mp[1].push_front({1,key,value});
mp[key]=freq_mp[1].begin();
}
};