-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path146.lru-缓存机制.go
More file actions
79 lines (72 loc) · 1.38 KB
/
146.lru-缓存机制.go
File metadata and controls
79 lines (72 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* @lc app=leetcode.cn id=146 lang=golang
*
* [146] LRU 缓存机制
*/
package leetcode
// @lc code=start
type Node struct {
val int
key int
pre *Node
next *Node
}
type LRUCache struct {
ht map[int]*Node
head *Node
tail *Node
cap int
size int
}
func Constructor(capacity int) LRUCache {
lru := LRUCache{cap: capacity, size: 0}
lru.ht = make(map[int]*Node)
lru.head = &Node{}
lru.tail = &Node{}
lru.head.next = lru.tail
lru.tail.pre = lru.head
return lru
}
func (this *LRUCache) Get(key int) int {
if node, ok := this.ht[key]; ok {
this.Refresh(node)
return node.val
}
return -1
}
func (this *LRUCache) Put(key int, value int) {
var node *Node
var ok bool
if node, ok = this.ht[key]; ok {
node.val = value
} else {
if this.size == this.cap {
del := this.head.next
del.next.pre = del.pre
del.pre.next = del.next
delete(this.ht, del.key)
this.size--
}
node = &Node{val: value, key: key}
this.ht[key] = node
this.size++
}
this.Refresh(node)
}
func (this *LRUCache) Refresh(node *Node) {
if node.next != nil {
node.next.pre = node.pre
node.pre.next = node.next
}
node.next = this.tail
node.pre = this.tail.pre
node.pre.next = node
this.tail.pre = node
}
/**
* Your LRUCache object will be instantiated and called as such:
* obj := Constructor(capacity);
* param_1 := obj.Get(key);
* obj.Put(key,value);
*/
// @lc code=end