LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get
and set
.
get(key)
- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value)
- Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
1 struct node{ 2 int key; 3 int value; 4 node* pre; 5 node* next; 6 }; 7 class LRUCache{ 8 private: 9 int count; 10 int size; 11 map<int,node*> mp; 12 node* cachehead; 13 node* cachetail; 14 public: 15 LRUCache(int capacity) { 16 count=0; 17 size=capacity; 18 19 cachehead=NULL; 20 cachetail=NULL; 21 } 22 23 int get(int key) { 24 if(cachehead==NULL) return -1; 25 map<int,node*>::iterator it=mp.find(key); 26 if(it==mp.end()) 27 return -1; 28 else 29 { 30 node* p=it->second; 31 pushfront(p); 32 //p->pre->next=p->next; 33 /*if(p->next!=NULL) 34 p->next->pre=p->pre; 35 mp.erase(p->key); 36 p->next=cachehead; 37 p->pre=cachehead->pre; 38 cachehead->pre=p; 39 cachehead=cachehead->pre; 40 mp[cachehead->key]=cachehead;*/ 41 42 } 43 return cachehead->value; 44 } 45 46 void set(int key, int value) { 47 if(cachehead==NULL) 48 { 49 cachehead=(node*)malloc(sizeof(node)); 50 cachehead->key=key; 51 cachehead->value=value; 52 cachehead->pre=NULL; 53 cachehead->next=NULL; 54 mp[key]=cachehead; 55 cachetail=cachehead; 56 count++; 57 } 58 else 59 { 60 map<int,node*>::iterator it=mp.find(key); 61 if(it==mp.end()) 62 { 63 if(count==size) 64 { 65 if(cachehead==cachetail&&cachehead!=NULL) 66 { 67 mp.erase(cachehead->key); 68 cachehead->key=key; 69 cachehead->value=value; 70 mp[key]=cachehead; 71 } 72 else 73 { 74 node* p=cachetail; 75 cachetail->pre->next=cachetail->next; 76 cachetail=cachetail->pre; 77 78 mp.erase(p->key); 79 80 p->key=key; 81 p->value=value; 82 83 p->next=cachehead; 84 p->pre=cachehead->pre; 85 cachehead->pre=p; 86 cachehead=p; 87 mp[cachehead->key]=cachehead; 88 } 89 90 } 91 else 92 { 93 node* p=(node*)malloc(sizeof(node)); 94 p->key=key; 95 p->value=value; 96 p->next=cachehead; 97 p->pre=cachehead->pre; 98 cachehead->pre=p; 99 cachehead=p; 100 mp.erase(p->key); 101 mp[cachehead->key]=cachehead; 102 count++; 103 } 104 } 105 else 106 { 107 108 node* p=it->second; 109 p->value=value; 110 pushfront(p); 111 /* mp.erase(p->key); 112 113 p->pre->next=p->next; 114 if(p->next!=NULL) 115 p->next->pre=p->pre; 116 p->value=value; 117 p->next=cachehead; 118 p->pre=cachehead->pre; 119 cachehead->pre=p; 120 cachehead=cachehead->pre; 121 mp[cachehead->key]=cachehead;*/ 122 } 123 } 124 125 126 } 127 void pushfront(node* cur) 128 { 129 if(count==1) 130 return; 131 if(cur==cachehead) return; 132 if(cur==cachetail) 133 cachetail=cur->pre; 134 cur->pre->next=cur->next; 135 if(cur->next!=NULL) 136 cur->next->pre=cur->pre; 137 cur->next=cachehead; 138 cur->pre=cachehead->pre; 139 cachehead->pre=cur; 140 cachehead=cur; 141 } 142 143 };
联系方式:emhhbmdfbGlhbmcxOTkxQDEyNi5jb20=
分类:
leetcode
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了