go语言实现LRU缓存

go语言实现LRU Cache

题目描述

设计和构建一个“最近最少使用”缓存,该缓存会删除最近最少使用的项目。缓存应该从键映射到值(允许你插入和检索特定键对应的值),并在初始化时指定最大容量。当缓存被填满时,它应该删除最近最少使用的项目。

它应该支持以下操作: 获取数据 get 和 写入数据 put 。

获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。
写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。

详细代码

type LRUCache struct {
   
	capacity   int
	m          map[int]*Node
	head, tail *Node
}

type Node struct {
   
	Key       int
	Value     int
	Pre, Next *Node
}

func Constructor(capacity int) LRUCache {
   
	head, tail := &Node{
   }, &Node{
   }
	head.Next = tail
	tail.Pre = head
	return LRUCache{
   
		capacity: capacity,
		m:        map[int]*Node{
   },
		head:     head,
		tail:     tail,
	}
}

func (this *LRUCache) Get(key int) int {
   
	// 存在,放到头
	if v, ok := this.m[key]; ok {
   
		this.moveToHead(v)
		return v.Value
	}
	// 不存在,返回-1
	return -1
}

func (this *LRUCache) Put(key int, value int) {
   
	// 已经存在了
	if v, ok := this.m[key];ok{
   
		v.Value = value
		this.moveToHead(v)
		return 
	}
	if this.capacity==len(this.m){
   
		rmKey := this.removeTail()
		delete(this.m ,rmKey)
	}
	newNode := &Node{
   Key: key, Value: value}
	this.m[key] = newNode
	this.addToHead(newNode)
}

func (this *LRUCache) moveToHead(node *Node) {
   
	this.deleteNode(node)
	this.addToHead(node)
}

func (this *LRUCache) deleteNode(node *Node) {
   
	node.Pre.Next = node.Next
	node.Next.Pre = node.Pre
}

func (this *LRUCache) addToHead(node *Node) {
   
	// 先让node位于现存第一位元素之前
	this.head.Next.Pre = node
	// 通过node的next指针让原始第一位元素放到第二位
	node.Next = this.head.Next
	// 捆绑node和head的关系
	this.head.Next = node
	node.Pre = this.head
}

func (this *LRUCache)removeTail()int{
   
	node := this.tail.Pre
    this.deleteNode(node)
    return node.Key
}

/**
 * Your LRUCache object will be instantiated and called as such:
 * obj := Constructor(capacity);
 * param_1 := obj.Get(key);
 * obj.Put(key,value);
 */

相关推荐

  1. go语言实现LRU缓存

    2024-02-10 11:24:02       31 阅读
  2. C# 实现Lru缓存

    2024-02-10 11:24:02       37 阅读
  3. C++实现一个LRU缓存

    2024-02-10 11:24:02       24 阅读
  4. LeetCode的LRU缓存实现

    2024-02-10 11:24:02       15 阅读
  5. 通过对象轮换实现 LRU 缓存结构

    2024-02-10 11:24:02       38 阅读
  6. 理解和实现 LRU 缓存置换算法

    2024-02-10 11:24:02       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-10 11:24:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-10 11:24:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-10 11:24:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-10 11:24:02       20 阅读

热门阅读

  1. 融资项目——配置redis

    2024-02-10 11:24:02       32 阅读
  2. 力扣热题100_哈希_49_字母异位词分组

    2024-02-10 11:24:02       36 阅读
  3. 2.8作业

    2024-02-10 11:24:02       28 阅读
  4. 最大优势(1e5)_题解

    2024-02-10 11:24:02       24 阅读
  5. LeetCode32. Longest Valid Parentheses——动态规划

    2024-02-10 11:24:02       27 阅读
  6. django中实现登录

    2024-02-10 11:24:02       35 阅读
  7. Linux学习

    2024-02-10 11:24:02       24 阅读
  8. 配置ARM交叉编译工具的通用步骤

    2024-02-10 11:24:02       29 阅读
  9. B站弹幕分析系统

    2024-02-10 11:24:02       32 阅读
  10. 蓝桥杯:大写

    2024-02-10 11:24:02       20 阅读