forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.go
105 lines (83 loc) · 1.97 KB
/
Solution.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
type list struct{ //双向链表,用于保存key:value
prev,next *list
key,val int
}
type LRUCache struct {
_len,_cap int
front,back *list //头尾指针
umap map[int]*list //hash表
}
func Constructor(capacity int) LRUCache {
return LRUCache{
_len : 0,
_cap : capacity,
front: nil,
back : nil,
umap : make(map[int]*list,capacity),
}
}
func (this *LRUCache) Get(key int) int {
if node,ok := this.umap[key];ok{ //存在
this.push_front(node)
return node.val
}
return -1
}
func (this *LRUCache) Put(key int, value int) {
if node,ok := this.umap[key];ok{
node.val = value
this.push_front(node)
return
}
//找不到
if this._len == this._cap{
delete(this.umap,this.back.key)
this.pop_back()
}else{
this._len++
}
node := &list{
prev:nil,
next:nil,
key:key,
val:value,
}
this.umap[key] = node
this.insert_front(node)
}
func (this *LRUCache) push_front(node *list){
switch node{ //先删除,再插入
case this.front:
return
case this.back:
this.pop_back()
default:
node.prev.next = node.next
node.next.prev = node.prev
}
this.insert_front(node)
}
func (this *LRUCache) pop_back(){
if this.back.prev != nil{ //链表长度大于1
this.back.prev.next = nil
}else{ //链表长度小于等于1
this.front = nil
}
this.back = this.back.prev
}
func (this *LRUCache)insert_front(node *list){
if this.back == nil{
//空表
this.back = node
}else{ //头插法
node.next = this.front
this.front.prev = node
}
this.front = node
}
/**
* Your LRUCache object will be instantiated and called as such:
* obj := Constructor(capacity);
* param_1 := obj.Get(key);
* obj.Put(key,value);
*/