forked from halfrost/LeetCode-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRUCache.go
139 lines (124 loc) · 2.71 KB
/
LRUCache.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package template
// LRUCache define
type LRUCache struct {
head, tail *Node
keys map[int]*Node
capacity int
}
// Node define
type Node struct {
key, val int
prev, next *Node
}
// ConstructorLRU define
func ConstructorLRU(capacity int) LRUCache {
return LRUCache{keys: make(map[int]*Node), capacity: capacity}
}
// Get define
func (lruCache *LRUCache) Get(key int) int {
if node, ok := lruCache.keys[key]; ok {
lruCache.Remove(node)
lruCache.Add(node)
return node.val
}
return -1
}
// Put define
func (lruCache *LRUCache) Put(key int, value int) {
node, ok := lruCache.keys[key]
if ok {
node.val = value
lruCache.Remove(node)
lruCache.Add(node)
return
}
node = &Node{key: key, val: value}
lruCache.keys[key] = node
lruCache.Add(node)
if len(lruCache.keys) > lruCache.capacity {
delete(lruCache.keys, lruCache.tail.key)
lruCache.Remove(lruCache.tail)
}
}
// Add define
func (lruCache *LRUCache) Add(node *Node) {
node.prev = nil
node.next = lruCache.head
if lruCache.head != nil {
lruCache.head.prev = node
}
lruCache.head = node
if lruCache.tail == nil {
lruCache.tail = node
lruCache.tail.next = nil
}
}
// Remove define
func (lruCache *LRUCache) Remove(node *Node) {
if node == lruCache.head {
lruCache.head = node.next
if node.next != nil {
node.next.prev = nil
}
node.next = nil
return
}
if node == lruCache.tail {
lruCache.tail = node.prev
node.prev.next = nil
node.prev = nil
return
}
node.prev.next = node.next
node.next.prev = node.prev
}
/**
* Your LRUCache object will be instantiated and called as such:
* obj := Constructor(capacity);
* param_1 := obj.Get(key);
* obj.Put(key,value);
*/
// 22%
// import "container/list"
// type LRUCache struct {
// Cap int
// Keys map[int]*list.Element
// List *list.List
// }
// type pair struct {
// K, V int
// }
// func Constructor(capacity int) LRUCache {
// return LRUCache{
// Cap: capacity,
// Keys: make(map[int]*list.Element),
// List: list.New(),
// }
// }
// func (c *LRUCache) Get(key int) int {
// if el, ok := c.Keys[key]; ok {
// c.List.MoveToFront(el)
// return el.Value.(pair).V
// }
// return -1
// }
// func (c *LRUCache) Put(key int, value int) {
// if el, ok := c.Keys[key]; ok {
// el.Value = pair{K: key, V: value}
// c.List.MoveToFront(el)
// } else {
// el := c.List.PushFront(pair{K: key, V: value})
// c.Keys[key] = el
// }
// if c.List.Len() > c.Cap {
// el := c.List.Back()
// c.List.Remove(el)
// delete(c.Keys, el.Value.(pair).K)
// }
// }
/**
* Your LRUCache object will be instantiated and called as such:
* obj := Constructor(capacity);
* param_1 := obj.Get(key);
* obj.Put(key,value);
*/