模拟题,思路简单,但是难是难在对c++ 的用法!!!要好好学习!!! 2刷还要解决超时问题和自己写一次!
class LRUCache{PRivate: typedef pair<int,list<int>::iterator> PILI; int capacity; map<int,PILI> datas; list<int> s;public: LRUCache(int capacity) { this->capacity=capacity; } int get(int key) { auto iter=datas.find(key); if(iter!=datas.end()) { update(iter); return iter->second.first; } else return -1; } void set(int key, int value) { int length=datas.size(); auto iter=datas.find(key); if(iter!=datas.end()) { iter->second.first=value; update(iter); } else { if(length>=capacity) { datas.erase(s.back()); s.pop_back(); } s.push_front(key); datas[key]=PILI(value,s.begin()); } } private: void update(map<int,PILI>::iterator iter) { int key=iter->first; s.erase(iter->second.second); s.push_front(key); iter->second.second=s.begin(); }};新闻热点
疑难解答