首页 > 代码库 > leetcode-LRU
leetcode-LRU
class LRUCache{public:struct node{ int key; int value; node(int k,int v):key(k),value(v){}}; LRUCache(int capacity1) { capacity = capacity1; } int get(int key) { unordered_map<int, node *>::iterator it = pos.find(key); if(it == pos.end()) return -1; node * check = it->second; int result = check->value; linked.remove(check); linked.push_front(check); } void set(int key, int value) { unordered_map<int, node *>::iterator it = pos.find(key); if(it != pos.end()) { node *t = it->second; t->value =http://www.mamicode.com/ value; linked.remove(t); linked.push_front(t); } else { node *t = new node(key, value); pos.insert(make_pair(key, t)); if(linked.size() < capacity) { linked.push_front(t); } else { node *temp = linked.back(); linked.pop_back(); linked.push_front(t); pos.erase(temp->key); delete temp; } } } private: int capacity; unordered_map<int, node *> pos; list<node *> linked;};
leetcode-LRU
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。