-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path146_LRUCache.cpp
More file actions
38 lines (33 loc) · 826 Bytes
/
Copy path146_LRUCache.cpp
File metadata and controls
38 lines (33 loc) · 826 Bytes
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
class LRUCache {
private:
int size=0;
unordered_map<int, int> lru;
//unordered_map<int,int> hash
list<int> cache;
public:
LRUCache(int capacity) {
size = capacity;
}
int get(int key) {
if(lru.count(key) == 0)
return -1;
cache.remove(key);
cache.push_front(key);
return lru[key];
}
void put(int key, int value) {
if(lru.size() == size && lru.count(key) == 0) {
lru.erase(cache.back());
cache.pop_back();
}
cache.remove(key);
cache.push_front(key);
lru[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);
*/