Skip to content

Commit 5322f72

Browse files
committed
feat: add golang solution to lc problem: No.0146
No.0146.Lru Cache
1 parent d63fb27 commit 5322f72

File tree

3 files changed

+196
-87
lines changed

3 files changed

+196
-87
lines changed

solution/0100-0199/0146.Lru Cache/README.md

+73
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,79 @@ impl LRUCache {
392392
}
393393
```
394394

395+
### **Go**
396+
397+
```go
398+
type node struct {
399+
key, val int
400+
prev, next *node
401+
}
402+
403+
type LRUCache struct {
404+
capacity int
405+
cache map[int]*node
406+
head, tail *node
407+
}
408+
409+
func Constructor(capacity int) LRUCache {
410+
head := new(node)
411+
tail := new(node)
412+
head.next = tail
413+
tail.prev = head
414+
return LRUCache{
415+
capacity: capacity,
416+
cache: make(map[int]*node, capacity),
417+
head: head,
418+
tail: tail,
419+
}
420+
}
421+
422+
func (this *LRUCache) Get(key int) int {
423+
n, ok := this.cache[key]
424+
if !ok {
425+
return -1
426+
}
427+
this.moveToFront(n)
428+
return n.val
429+
}
430+
431+
func (this *LRUCache) Put(key int, value int) {
432+
n, ok := this.cache[key]
433+
if ok {
434+
n.val = value
435+
this.moveToFront(n)
436+
return
437+
}
438+
if len(this.cache) == this.capacity {
439+
back := this.tail.prev
440+
this.remove(back)
441+
delete(this.cache, back.key)
442+
}
443+
n = &node{key: key, val: value}
444+
this.pushFront(n)
445+
this.cache[key] = n
446+
}
447+
448+
func (this *LRUCache) moveToFront(n *node) {
449+
this.remove(n)
450+
this.pushFront(n)
451+
}
452+
453+
func (this *LRUCache) remove(n *node) {
454+
n.prev.next = n.next
455+
n.next.prev = n.prev
456+
n.prev = nil
457+
n.next = nil
458+
}
459+
460+
func (this *LRUCache) pushFront(n *node) {
461+
n.prev = this.head
462+
n.next = this.head.next
463+
this.head.next.prev = n
464+
this.head.next = n
465+
}
466+
```
467+
395468
### **...**
396469

397470
```

solution/0100-0199/0146.Lru Cache/README_EN.md

+73
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,79 @@ impl LRUCache {
341341
}
342342
```
343343

344+
### **Go**
345+
346+
```go
347+
type node struct {
348+
key, val int
349+
prev, next *node
350+
}
351+
352+
type LRUCache struct {
353+
capacity int
354+
cache map[int]*node
355+
head, tail *node
356+
}
357+
358+
func Constructor(capacity int) LRUCache {
359+
head := new(node)
360+
tail := new(node)
361+
head.next = tail
362+
tail.prev = head
363+
return LRUCache{
364+
capacity: capacity,
365+
cache: make(map[int]*node, capacity),
366+
head: head,
367+
tail: tail,
368+
}
369+
}
370+
371+
func (this *LRUCache) Get(key int) int {
372+
n, ok := this.cache[key]
373+
if !ok {
374+
return -1
375+
}
376+
this.moveToFront(n)
377+
return n.val
378+
}
379+
380+
func (this *LRUCache) Put(key int, value int) {
381+
n, ok := this.cache[key]
382+
if ok {
383+
n.val = value
384+
this.moveToFront(n)
385+
return
386+
}
387+
if len(this.cache) == this.capacity {
388+
back := this.tail.prev
389+
this.remove(back)
390+
delete(this.cache, back.key)
391+
}
392+
n = &node{key: key, val: value}
393+
this.pushFront(n)
394+
this.cache[key] = n
395+
}
396+
397+
func (this *LRUCache) moveToFront(n *node) {
398+
this.remove(n)
399+
this.pushFront(n)
400+
}
401+
402+
func (this *LRUCache) remove(n *node) {
403+
n.prev.next = n.next
404+
n.next.prev = n.prev
405+
n.prev = nil
406+
n.next = nil
407+
}
408+
409+
func (this *LRUCache) pushFront(n *node) {
410+
n.prev = this.head
411+
n.next = this.head.next
412+
this.head.next.prev = n
413+
this.head.next = n
414+
}
415+
```
416+
344417
### **...**
345418

346419
```
+50-87
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,68 @@
1-
type list struct{ //双向链表,用于保存key:value
2-
prev,next *list
3-
key,val int
1+
type node struct {
2+
key, val int
3+
prev, next *node
44
}
55

66
type LRUCache struct {
7-
_len,_cap int
8-
front,back *list //头尾指针
9-
umap map[int]*list //hash表
7+
capacity int
8+
cache map[int]*node
9+
head, tail *node
1010
}
11-
1211

1312
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-
}
13+
head := new(node)
14+
tail := new(node)
15+
head.next = tail
16+
tail.prev = head
17+
return LRUCache{
18+
capacity: capacity,
19+
cache: make(map[int]*node, capacity),
20+
head: head,
21+
tail: tail,
22+
}
2123
}
2224

23-
2425
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
26+
n, ok := this.cache[key]
27+
if !ok {
28+
return -1
29+
}
30+
this.moveToFront(n)
31+
return n.val
3232
}
3333

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)
34+
func (this *LRUCache) Put(key int, value int) {
35+
n, ok := this.cache[key]
36+
if ok {
37+
n.val = value
38+
this.moveToFront(n)
39+
return
40+
}
41+
if len(this.cache) == this.capacity {
42+
back := this.tail.prev
43+
this.remove(back)
44+
delete(this.cache, back.key)
45+
}
46+
n = &node{key: key, val: value}
47+
this.pushFront(n)
48+
this.cache[key] = n
5949
}
6050

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)
51+
func (this *LRUCache) moveToFront(n *node) {
52+
this.remove(n)
53+
this.pushFront(n)
7454
}
7555

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
56+
func (this *LRUCache) remove(n *node) {
57+
n.prev.next = n.next
58+
n.next.prev = n.prev
59+
n.prev = nil
60+
n.next = nil
8461
}
8562

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
63+
func (this *LRUCache) pushFront(n *node) {
64+
n.prev = this.head
65+
n.next = this.head.next
66+
this.head.next.prev = n
67+
this.head.next = n
9768
}
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)