写的LRU通不过大数据,帮忙看看# JobHunting - 待字闺中
H*a
1 楼
用的list和unordered_map
----------------------------
谢谢提醒,改过之后,发现还是通不过
class LRUCache{
public:
int cap;
unordered_map mymap;
list mylist;
LRUCache(int capacity) {
cap = capacity;
}
int get(int key) {
unordered_map::iterator it = mymap.find(key);
if (it == mymap.end())
return -1;
else {
list::iterator it_list = mylist.begin();
while(*it_list != key) it_list++;
mylist.erase(it_list);
mylist.push_back(key);
return it->second;
}
}
void set(int key, int value) {
unordered_map::iterator it = mymap.find(key);
if (it == mymap.end()) {
if (mymap.size() < cap) {
mymap[key] = value;
mylist.push_back(key);
}
else {
int key_delete = mylist.front();
mylist.pop_front();
unordered_map::iterator it_delete = mymap.find(key
_delete);
mymap.erase(it_delete);
mylist.push_back(key);
mymap[key] = value;
}
}
else {
mymap[key] = value;
list::iterator it_list = mylist.begin();
while(*it_list != key) it_list++;
mylist.erase(it_list);
mylist.push_back(key);
}
}
};
----------------------------
谢谢提醒,改过之后,发现还是通不过
class LRUCache{
public:
int cap;
unordered_map
list
LRUCache(int capacity) {
cap = capacity;
}
int get(int key) {
unordered_map
if (it == mymap.end())
return -1;
else {
list
while(*it_list != key) it_list++;
mylist.erase(it_list);
mylist.push_back(key);
return it->second;
}
}
void set(int key, int value) {
unordered_map
if (it == mymap.end()) {
if (mymap.size() < cap) {
mymap[key] = value;
mylist.push_back(key);
}
else {
int key_delete = mylist.front();
mylist.pop_front();
unordered_map
_delete);
mymap.erase(it_delete);
mylist.push_back(key);
mymap[key] = value;
}
}
else {
mymap[key] = value;
list
while(*it_list != key) it_list++;
mylist.erase(it_list);
mylist.push_back(key);
}
}
};