Skip to content

Commit 63f4513

Browse files
committed
add 146.LRUCache Solution in CPP and GO
1 parent 625443f commit 63f4513

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

solution/0146.Lru Cache/Solution.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class LRUCache {
2+
private:
3+
int _cap;
4+
// 双链表:装着 (key, value) 元组
5+
list<pair<int, int>> cache;
6+
// 哈希表:key 映射到 (key, value) 在 cache 中的位置
7+
unordered_map<int, list<pair<int, int>>::iterator> umap;
8+
public:
9+
LRUCache(int capacity) {
10+
_cap = capacity;
11+
12+
}
13+
14+
int get(int key) {
15+
auto iter = umap.find(key);
16+
if(iter == umap.end())return -1;
17+
18+
pair<int,int> kv = *umap[key];
19+
cache.erase(umap[key]);//
20+
cache.push_front(kv);
21+
umap[key] = cache.begin();
22+
23+
return kv.second;
24+
}
25+
26+
void put(int key, int value) {
27+
auto iter = umap.find(key);
28+
if(iter != umap.end()){ //存在于缓存
29+
30+
cache.erase(umap[key]);
31+
cache.push_front(make_pair(key,value));
32+
umap[key] = cache.begin();
33+
34+
return;
35+
}
36+
37+
38+
//不在缓存中
39+
if(cache.size() == _cap) //满了
40+
{
41+
42+
auto iter = cache.back();
43+
umap.erase(iter.first);
44+
cache.pop_back();
45+
46+
cache.push_front(make_pair(key,value));
47+
umap[key] = cache.begin();
48+
49+
}
50+
else //没满
51+
{
52+
cache.push_front(make_pair(key,value));
53+
umap[key] = cache.begin();
54+
55+
}
56+
}
57+
};

solution/0146.Lru Cache/Solution.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
type list struct{ //双向链表,用于保存key:value
2+
prev,next *list
3+
key,val int
4+
}
5+
6+
type LRUCache struct {
7+
_len,_cap int
8+
front,back *list //头尾指针
9+
umap map[int]*list //hash表
10+
}
11+
12+
13+
func Constructor(capacity int) LRUCache {
14+
return LRUCache{
15+
_len : 0,
16+
_cap : capacity,
17+
front: nil,
18+
back : nil,
19+
umap : make(map[int]*list,capacity),
20+
}
21+
}
22+
23+
24+
func (this *LRUCache) Get(key int) int {
25+
if node,ok := this.umap[key];ok{ //存在
26+
27+
this.push_front(node)
28+
return node.val
29+
}
30+
31+
return -1
32+
}
33+
34+
35+
func (this *LRUCache) Put(key int, value int) {
36+
if node,ok := this.umap[key];ok{
37+
node.val = value
38+
this.push_front(node)
39+
return
40+
}
41+
42+
//找不到
43+
if this._len == this._cap{
44+
delete(this.umap,this.back.key)
45+
this.pop_back()
46+
}else{
47+
this._len++
48+
}
49+
50+
node := &list{
51+
prev:nil,
52+
next:nil,
53+
key:key,
54+
val:value,
55+
}
56+
57+
this.umap[key] = node
58+
this.insert_front(node)
59+
}
60+
61+
62+
func (this *LRUCache) push_front(node *list){
63+
switch node{ //先删除,再插入
64+
case this.front:
65+
return
66+
case this.back:
67+
this.pop_back()
68+
default:
69+
node.prev.next = node.next
70+
node.next.prev = node.prev
71+
}
72+
73+
this.insert_front(node)
74+
}
75+
76+
func (this *LRUCache) pop_back(){
77+
if this.back.prev != nil{ //链表长度大于1
78+
this.back.prev.next = nil
79+
}else{ //链表长度小于等于1
80+
this.front = nil
81+
}
82+
83+
this.back = this.back.prev
84+
}
85+
86+
87+
func (this *LRUCache)insert_front(node *list){
88+
if this.back == nil{
89+
//空表
90+
this.back = node
91+
}else{ //头插法
92+
node.next = this.front
93+
this.front.prev = node
94+
}
95+
96+
this.front = node
97+
}
98+
99+
100+
/**
101+
* Your LRUCache object will be instantiated and called as such:
102+
* obj := Constructor(capacity);
103+
* param_1 := obj.Get(key);
104+
* obj.Put(key,value);
105+
*/

0 commit comments

Comments
 (0)