标签:cond map called instant erase == pop leetcode 两种
class LRUCache { public: LRUCache(int capacity) { cap = capacity; } int get(int key) { auto it = map1.find(key); if(it == map1.end()) return -1; list1.splice(list1.begin(), list1, it->second); return it->second->second; } void put(int key, int value) { auto it = map1.find(key); if(it != map1.end()) { list1.erase(it->second); } if(list1.size() == cap) { int k = list1.rbegin()->first; list1.pop_back(); map1.erase(k); } list1.push_front(make_pair(key, value)); map1[key] = list1.begin(); } private: int cap; list<pair<int, int>> list1; unordered_map<int, list<pair<int, int>>::iterator> map1; }; /** * 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); */
标签:cond map called instant erase == pop leetcode 两种
原文地址:https://www.cnblogs.com/xumaomao/p/11359692.html