We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
classLRUCache:publicCache{public:LRUCache(intcap){capacity=cap;}voidset(intkey,intval){map<int,Node*>::iteratorit=mp.find(key);if(it==mp.end()){// not in the cacheNode*newNode=newNode(key,val);mp.emplace(key,newNode);if(mp.size()==1){head=newNode;tail=newNode;}else{head->prev=newNode;newNode->next=head;head=newNode;if(mp.size()>capacity){Node*last=tail;tail=last->prev;mp.erase(last->key);tail->next=nullptr;deletelast;}}}else{// cache hitNode*node=it->second;node->value=val;if(mp.size()!=1){if(node->prev!=nullptr){node->prev->next=node->next;head->prev=node;node->prev=nullptr;head=node;}}}}intget(intkey){map<int,Node*>::iteratorit=mp.find(key);return(it==mp.end()?-1:it->second->value);}private:intcapacity;};
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Abstract Classes - Polymorphism
You are viewing a single comment's thread. Return to all comments →
Here is my solution: