-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlru-cache.cpp
More file actions
74 lines (73 loc) · 1.76 KB
/
lru-cache.cpp
File metadata and controls
74 lines (73 loc) · 1.76 KB
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
// link :- https://leetcode.com/problems/lru-cache/
class node{
public:
int key, value;
struct node *next, *prev;
node(int key,int value){
this->key=key;
this->value=value;
next=NULL,prev=NULL;
}
};
class LRUCache {
private:
int sz,cursz;
node *head,*tail;
map<int,node *>address;
public:
LRUCache(int capacity) {
sz=capacity;
cursz=0;
head = new node(-1,-1);
tail = new node(-1,-1);
head->next = tail;
tail->prev = head;
}
void remove(node *tm,bool choice){
node * tml = tm->prev,*tmr = tm->next;
tml->next=tmr;
tmr->prev=tml;
if(choice)
delete(tm);
}
void insert_front(node* tm){
node *tmr=head->next,*tml=head;
tml->next=tm;tm->prev=tml;
tm->next=tmr;tmr->prev=tm;
}
int get(int key) {
// return -2;
if(address.find(key)==address.end()){
return -1;
}
node *tm = address[key];
remove(tm,0);
insert_front(tm);
return tm->value;
}
void put(int key, int val) {
// return;
if(address.find(key)==address.end()){
if(cursz==sz){
address.erase(tail->prev->key);
remove(tail->prev,1);
cursz--;
}
cursz++;
node *tm = new node(key,val);
insert_front(tm);
address[key]=tm;
}else {
node *tm = address[key];
remove(tm,0);
tm->value=val;
insert_front(tm);
}
}
};
/**
* 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);
*/