dengxinyi 6 rokov pred
rodič
commit
8313f38a76
2 zmenil súbory, kde vykonal 34 pridanie a 34 odobranie
  1. 16 16
      hard/146.lru-cache.go
  2. 18 18
      hard/460.lfu-cache.go

+ 16 - 16
hard/146.lru-cache.go

@@ -1,29 +1,29 @@
-type biNode struct {
+type listNode struct {
 	key int
 	val int
-	prev *biNode
-	next *biNode
+	prev *listNode
+	next *listNode
 }
 
-type biList struct {
-	head *biNode
-	tail *biNode
+type linkedList struct {
+	head *listNode
+	tail *listNode
 }
 
-func newBiList() *biList {
-	head, tail := &biNode{}, &biNode{}
+func newLinkedList() *linkedList {
+	head, tail := &listNode{}, &listNode{}
 	head.next = tail
 	tail.prev = head
-	li := &biList{head, tail}
+	li := &linkedList{head, tail}
 	return li
 }
 
-func (li *biList) remove(node *biNode) {
+func (li *linkedList) remove(node *listNode) {
 	node.prev.next = node.next
 	node.next.prev = node.prev
 }
 
-func (li *biList) insert(node *biNode) {
+func (li *linkedList) insert(node *listNode) {
 	node.next = li.head.next
 	node.prev = li.head
 	li.head.next = node
@@ -33,15 +33,15 @@ func (li *biList) insert(node *biNode) {
 type LRUCache struct {
 	capacity int
 	size     int
-	cache    map[int]*biNode
-	list     *biList
+	cache    map[int]*listNode
+	list     *linkedList
 }
 
 func Constructor(capacity int) LRUCache {
 	var lru LRUCache
 	lru.capacity = capacity
-	lru.cache = make(map[int]*biNode)
-	lru.list = newBiList()
+	lru.cache = make(map[int]*listNode)
+	lru.list = newLinkedList()
 	return lru
 }
 
@@ -70,7 +70,7 @@ func (this *LRUCache) Put(key int, value int)  {
 		this.list.remove(node)
 		this.size--
 	}
-	newNode := &biNode{key, value, nil, nil}
+	newNode := &listNode{key, value, nil, nil}
 	this.list.insert(newNode)
 	this.cache[key] = newNode
 	this.size++

+ 18 - 18
hard/460.lfu-cache.go

@@ -1,25 +1,25 @@
-type biNode struct {
+type listNode struct {
 	key  int
 	val  int
 	cnt  int
-	prev *biNode
-	next *biNode
+	prev *listNode
+	next *listNode
 }
 
-type biList struct {
-	head *biNode
-	tail *biNode
+type linkedList struct {
+	head *listNode
+	tail *listNode
 }
 
-func newBiList() *biList {
-	head, tail := &biNode{}, &biNode{}
+func newLinkedList() *linkedList {
+	head, tail := &listNode{}, &listNode{}
 	head.next = tail
 	tail.prev = head
-	li := &biList{head, tail}
+	li := &linkedList{head, tail}
 	return li
 }
 
-func (li *biList) remove(node *biNode) {
+func (li *linkedList) remove(node *listNode) {
 	node.prev.next = node.next
 	node.next.prev = node.prev
 }
@@ -28,15 +28,15 @@ type LFUCache struct {
 	capacity int
 	size     int
 	min      int
-	cache    map[int]*biNode
-	freq     map[int]*biList
+	cache    map[int]*listNode
+	freq     map[int]*linkedList
 }
 
 func Constructor(capacity int) LFUCache {
 	var lfu LFUCache
 	lfu.capacity = capacity
-	lfu.cache = make(map[int]*biNode)
-	lfu.freq = make(map[int]*biList)
+	lfu.cache = make(map[int]*listNode)
+	lfu.freq = make(map[int]*linkedList)
 	return lfu
 }
 
@@ -66,17 +66,17 @@ func (this *LFUCache) Put(key int, value int) {
 		}
 		this.size--
 	} // If the cache is full, remove the last element in the least freq list.
-	newNode := &biNode{key, value, 1, nil, nil}
+	newNode := &listNode{key, value, 1, nil, nil}
 	this.insert(newNode)
 	this.cache[key] = newNode
 	this.min = 1
 	this.size++ // Then, insert new node.
 }
 
-func (this *LFUCache) insert(node *biNode) {
+func (this *LFUCache) insert(node *listNode) {
 	li := this.freq[node.cnt]
 	if li == nil {
-		li = newBiList()
+		li = newLinkedList()
 		this.freq[node.cnt] = li
 	}
 	node.next = li.head.next
@@ -85,7 +85,7 @@ func (this *LFUCache) insert(node *biNode) {
 	node.next.prev = node
 }
 
-func (this *LFUCache) touch(node *biNode) {
+func (this *LFUCache) touch(node *listNode) {
 	li := this.freq[node.cnt]
 	li.remove(node)
 	if li.head.next == li.tail {