思路
- 用列表实现,有head和tail
- 为什么需要tail?
- 为什么node节点需要key?
- 当get值的时候,用map查找
- 当put值的时候
- 如果key已经存在则需要更新val,同时将node调整到表头
- 如果key不存在则新建node,将其调整到表头
代码
package main
import "fmt"
type LRUCache struct {
cap int
num int
head *LRUMem
tail *LRUMem
values map[int]*LRUMem
}
type LRUMem struct {
pre *LRUMem
next *LRUMem
val int
key int
}
func Constructor(capacity int) LRUCache {
return LRUCache {
cap: capacity,
num: 0,
head: nil,
tail: nil,
values: make(map[int]*LRUMem, capacity),
}
}
func (this *LRUCache) Get(key int) int {
v, ok := this.values[key]
if !ok {
return -1
}
if v == this.head {
return v.val
}
if v.pre != nil {
v.pre.next = v.next
if v.next == nil {
this.tail = v.pre
}
}
if v.next != nil {
v.next.pre = v.pre
}
v.pre = nil
v.next = this.head
this.head.pre = v
this.head = v
return v.val
}
func (this *LRUCache) Put(key int, value int) {
if this.cap == 0 {
return
}
_, ok := this.values[key]
if ok {
this.values[key].val = value
this.Get(key)
return
}
this.num = this.num+1
newMem := &LRUMem {
pre: nil,
next: nil,
val: value,
key: key,
}
newMem.next = this.head
if this.head != nil {
this.head.pre = newMem
}
if this.num == 1 {
this.tail = newMem
}
if this.num == 2 {
this.tail = this.head
}
this.head = newMem
if this.num == this.cap+1 {
if this.tail != nil {
needDel := this.tail
if needDel.pre != nil {
needDel.pre.next = nil
this.tail = needDel.pre
this.tail.next = nil
}
delete(this.values, needDel.key)
this.num = this.num-1
}
}
this.values[key] = newMem
}
func (this *LRUCache) Print() {
if this.head != nil {
fmt.Println("head:", this.head.key)
}
if this.tail != nil {
fmt.Println("tail:", this.tail.key)
}
for node:= this.head; node!= nil ;node= node.next {
fmt.Println(node.key, node.val)
}
fmt.Println("********************")
}
func main() {
lRUCache := Constructor(3);
lRUCache.Put(1, 1);
lRUCache.Put(2, 2);
lRUCache.Put(3, 3);
lRUCache.Put(4, 4);
lRUCache.Get(4);
lRUCache.Get(3);
lRUCache.Put(5, 5);
}
|