MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / Put

Method Put

go/0146-lru-cache.go:49–62  ·  view source on GitHub ↗
(key int, value int)

Source from the content-addressed store, hash-verified

47
48
49func (this *LRUCache) Put(key int, value int) {
50 if _, ok := this.cache[key]; ok {
51 this.Remove(this.cache[key])
52 }
53 this.cache[key] = &Node{key: key, val: value}
54 this.Insert(this.cache[key])
55
56 if len(this.cache) > this.capacity {
57 // remove from the list and delete the LRU from hashmap
58 lru := this.left.next
59 this.Remove(lru)
60 delete(this.cache, lru.key)
61 }
62}

Callers

nothing calls this directly

Calls 2

RemoveMethod · 0.95
InsertMethod · 0.95

Tested by

no test coverage detected