前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【每日一算法】(三)使用数组+ map实现letcoode 031题 LRU 缓存

【每日一算法】(三)使用数组+ map实现letcoode 031题 LRU 缓存

作者头像
被测试耽误的大厨
发布2023-11-17 11:00:26
1570
发布2023-11-17 11:00:26
举报
文章被收录于专栏:测试平台系列测试平台系列

题目:

运用所掌握的数据结构,设计和实现一个 LRU (Least Recently Used,最近最少使用) 缓存机制 。

实现 LRUCache 类:

  • LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存
  • int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1
  • void put(int key, int value) 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。

示例:

代码语言:javascript
复制
输入
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
输出
[null, null, null, 1, null, -1, null, -1, 3, 4]

解释
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // 缓存是 {1=1}
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
lRUCache.get(1);    // 返回 1
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
lRUCache.get(2);    // 返回 -1 (未找到)
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
lRUCache.get(1);    // 返回 -1 (未找到)
lRUCache.get(3);    // 返回 3
lRUCache.get(4);    // 返回 4
代码语言:javascript
复制
// 使用list+map 完成
type LRUCache struct {
  length   int
  cap      int
  keyCache []int
  cache    map[int]int
}

func Constructor(capacity int) LRUCache {
  return LRUCache{
    cap:   capacity,
    cache: make(map[int]int),
  }
}

func (lc *LRUCache) Get(key int) int {
  value := lc.isExist(key)
  lc.sort(key)
  return value
}

func (lc *LRUCache) Put(key int, value int) {
  lc.insert(key, value)
}

func (lc *LRUCache) len() int {
  if lc.cache != nil {
    return lc.length
  }
  lc.cache = make(map[int]int)
  return 0
}

func (lc *LRUCache) insert(key int, value int) {
  if lc.cache == nil {
    if lc.cap == 0 {
      lc.cap = 10
    }
    lc.cache = make(map[int]int)
  }

  if lc.isExist(key) != -1 {
    lc.sort(key)
  } else {
    var temp []int
    temp = append(temp, key)
    lc.keyCache = append(temp, lc.keyCache...)
    if lc.length < lc.cap {
      lc.length++
    } else {
      for _, k := range lc.keyCache[lc.length:len(lc.keyCache)] {
        delete(lc.cache, k)
      }
      lc.keyCache = lc.keyCache[:lc.length]
    }
  }
  lc.cache[key] = value

}

func (lc *LRUCache) sort(key int) {
  if lc.cache == nil {
    if lc.cap == 0 {
      lc.cap = 10
    }
    lc.cache = make(map[int]int)
  }
  index := lc.find(key)
  if index == -1 {
    return
  }
  var tempPre []int
  tempPre = append(tempPre, key)
  var tempNext []int
  if lc.length > index+1 {
    tempNext = append(tempNext, lc.keyCache[index+1:lc.length]...)
  }

  lc.keyCache = append(tempPre, lc.keyCache[0:index]...)
  lc.keyCache = append(lc.keyCache, tempNext...)

}

func (lc *LRUCache) find(key int) int {
  for index, k := range lc.keyCache {
    if k == key {
      return index
    }
  }
  return -1
}

func (lc *LRUCache) isExist(key int) int {
  if value, ok := lc.cache[key]; ok {
    return value
  } else {
    return -1
  }
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-10-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 全栈测试开发之路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档