标签:data lru type case content 移除 复杂 lis 设计
版本一、
func LRU( operators [][]int , k int ) []int { // write code here res := make([]int,0,len(operators)) key := make([]int,k) val := make([]int,k) for _,va := range operators{ switch{ case va[0] == 1: if len(key)==k{ key = key[1:] val = val[1:] } key = append(key, va[1]) val = append(val, va[2]) break case va[0] == 2 : idx := -1 for i:=0;i<len(key);i++{ if key[i] == va[1]{ idx = i break } } if idx == -1{ res = append(res, -1) }else{ res = append(res, val[idx]) if idx<k-1{ key = append(key[:idx], append(key[idx+1:],key[idx])...) val = append(val[:idx], append(val[idx+1:],val[idx])...) } } break } } return res }
版本二、
package main /** * lru design * @param operators int整型二维数组 the ops * @param k int整型 the k * @return int整型一维数组 */ type Data struct { Key int Value int } type Node struct { data *Data pre *Node next *Node } type LRUStruct struct { head *Node tail *Node lens int hashMap map[int]*Node capacity int } func (lruS *LRUStruct) InitLRU(capa int) { lruS.hashMap = make(map[int]*Node) lruS.capacity = capa } func (lruS *LRUStruct) Insert(key, value int) { if useV, ok := lruS.hashMap[key]; ok { useV.data.Value = value lruS.Delete(useV) lruS.InsertTop(useV) } else { newNode := &Node{ data: &Data{key, value}, } lruS.InsertTop(newNode) } } func (lruS *LRUStruct) Get(key int) int { if useV, ok := lruS.hashMap[key]; ok { lruS.Delete(useV) lruS.InsertTop(useV) return useV.data.Value } return -1 } func (lruS *LRUStruct) Delete(node *Node) { if node == lruS.head { lruS.head = lruS.head.next } if node == lruS.tail { lruS.tail = lruS.tail.pre } if nil != node.pre { node.pre.next = node.next } if nil != node.next { node.next.pre = node.pre } node.pre = nil node.next = nil delete(lruS.hashMap, node.data.Key) lruS.lens-- } func (lruS *LRUStruct) InsertTop(node *Node) { node.next = lruS.head if nil != lruS.head { lruS.head.pre = node } if nil == lruS.tail { lruS.tail = node } lruS.head = node lruS.lens++ if lruS.lens > lruS.capacity { lruS.Delete(lruS.tail) } lruS.hashMap[node.data.Key] = node } func LRU(operators [][]int, k int) []int { var resultList []int var lrus LRUStruct lrus.InitLRU(k) for _, v := range operators { switch v[0] { case 1: lrus.Insert(v[1], v[2]) case 2: resultList = append(resultList, lrus.Get(v[1])) } //lrus.Dump() } return resultList }
标签:data lru type case content 移除 复杂 lis 设计
原文地址:https://www.cnblogs.com/lvpengbo/p/14449885.html