classNode { public: int key; int value; Node* pre; Node* next; Node(int key, int _value): key(key), value(_value), pre(nullptr), next(nullptr) {} Node(int key, int value, Node* pre, Node* next) { this -> key = key; this -> value = value; this -> pre = pre; this -> next = next; } };
classLRUCache { public: int size; int capacity; Node* head; Node* tail; unordered_map<int, Node*> mp; LRUCache(int capacity) { this -> capacity = capacity; this -> size = 0; this -> head = newNode(-1, -1); this -> tail = newNode(-1, -1); head -> next = tail; tail -> pre = head; }
voidremoveNode(Node* node){ node -> pre -> next = node -> next; node -> next -> pre = node -> pre; }
voidaddNode(Node* node){ node -> pre = tail -> pre; node -> next = tail;
tail -> pre -> next = node; tail -> pre = node; } intget(int key){ if(mp.count(key)) { Node* temp = mp[key];
/** * 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); */