-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
80 lines (74 loc) · 973 Bytes
/
main.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
package main
import (
"fmt"
)
type lru struct{
list []int
head int
tail int
cap int
have bool
}
func (l *lru) put(n int){
i := l.head
for {
if l.list[i] == n {
t := i
j := (i+1)%l.cap
for {
if j == l.tail {
l.list[t] = n
return
}else {
l.list[t] = l.list[j]
}
t = j
j = (j+1)%l.cap
}
}
i = (i+1)%l.cap
if i == l.tail {
l.list[i] = n
l.tail = (l.tail + 1)%l.cap
if i == l.head && l.have {
l.head = (l.head+ 1)%l.cap
}
l.have = true
return
}
}
}
func (l *lru) print (){
i := l.head
for {
fmt.Print(l.list[i])
i = (i+1)%l.cap
if i == l.tail {
break
}
}
fmt.Println()
}
func main() {
l := &lru{list:make([]int, 3, 3), cap:3}
l.list[0] = -1
l.list[1] = -1
l.list[2] = -1
//7 0 1 2 0 3 0 4
l.put(7)
l.print()
l.put(0)
l.print()
l.put(1)
l.print()
l.put(2)
l.print()
l.put(0)
l.print()
l.put(3)
l.print()
l.put(0)
l.print()
l.put(4)
l.print()
}