请设计 最近 最少使用 约束的数据结构
20250307 39 分钟调试完成,需要确认的是,List初始化需要为空,而不是orderList: make([]int, capacity) 这个是创建capacity个0
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 type LRUCache struct { orderList []int cacheMaps map [int ]int capacity int } func Constructor (capacity int ) LRUCache { return LRUCache{ orderList: make ([]int , 0 , capacity), cacheMaps: make (map [int ]int , capacity), capacity: capacity, } } func (this *LRUCache) Get(key int ) int { if _, ok := this.cacheMaps[key]; ok { this.Update(key) return this.cacheMaps[key] } return -1 } func (this *LRUCache) Update(key int ) { for i := 0 ; i < len (this.orderList); i++ { if this.orderList[i] == key { this.orderList = append (this.orderList[:i], append (this.orderList[i+1 :], this.orderList[i])...) } } } func (this *LRUCache) Put(key int , value int ) { if _, ok := this.cacheMaps[key]; ok { this.cacheMaps[key] = value this.Update(key) } else if len (this.cacheMaps) >= this.capacity { fmt.Println("=" ,len (this.orderList), len (this.cacheMaps), this.capacity) oldKey := this.orderList[0 ] this.orderList = this.orderList[1 :] delete (this.cacheMaps, oldKey) this.cacheMaps[key] = value this.orderList = append (this.orderList, key) }else if len (this.cacheMaps) < this.capacity { fmt.Println("<" , len (this.orderList), len (this.cacheMaps), this.capacity) this.cacheMaps[key] = value this.orderList = append (this.orderList, key) } }
O(1) 方法双向列表,map 中直接保存列表元素指针, 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 type LRUCache struct { cacheMaps map [int ]*list.Element orderList *list.List capacity int } type entry struct { key int value int } func Constructor (capacity int ) LRUCache { return LRUCache{ cacheMaps: make (map [int ]*list.Element, capacity), orderList: list.New(), capacity: capacity, } } func (this *LRUCache) Get(key int ) int { if elem, ok := this.cacheMaps[key]; ok { this.orderList.MoveToBack(elem) return elem.Value.(entry).value } return -1 } func (this *LRUCache) Put(key int , value int ) { if elem, ok := this.cacheMaps[key]; ok { elem.Value = entry{key: key, value: value} this.orderList.MoveToBack(elem) } else { if len (this.cacheMaps) == this.capacity { frontElem := this.orderList.Front() delete (this.cacheMaps, frontElem.Value.(entry).key) this.orderList.Remove(frontElem) } newElem := this.orderList.PushBack(entry{key: key, value: value}) this.cacheMaps[key] = newElem } }
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 package leetcode1700 func countStudents (students []int , sandwiches []int ) int { for !isEnd(students, sandwiches) { if sandwiches[0 ] == students[0 ] { sandwiches = sandwiches[1 :] students = students[1 :] } else { students = append (students[1 :], students[0 ]) } } return len (students) } func isEnd (students []int , sandwiches []int ) bool { for _, val := range students { if val == sandwiches[0 ] { return false } } return true }