Skip to content

Commit

Permalink
Create 146.LRU-Cache.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Jul 8, 2017
1 parent 943c28a commit a0cc867
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Design/146.LRU-Cache/146.LRU-Cache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class LRUCache {
unordered_map<int,int>Map;
list<int>List;
unordered_map<int,list<int>::iterator>iter;
int cap;

public:
LRUCache(int capacity)
{
cap=capacity;
}

int get(int key)
{
if (Map.find(key)==Map.end()) return -1;

List.erase(iter[key]);
List.push_back(key);
iter[key]=--List.end();
return Map[key];
}

void put(int key, int value)
{
if (cap<=0) return;
if (get(key)!=-1)
{
Map[key]=value;
return;
}

if (List.size()<cap)
{
List.push_back(key);
iter[key]=--List.end();
Map[key]=value;
}
else
{
int minKey = List.front();
iter.erase(minKey);
List.pop_front();
Map.erase(minKey);

List.push_back(key);
iter[key]=--List.end();
Map[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);
*/

0 comments on commit a0cc867

Please sign in to comment.