leetcode 146. LRU缓存机制 golang实现

本文详细介绍了一种基于双向链表和哈希映射的数据结构,实现了一个高效、支持O(1)时间复杂度的LRU(最近最少使用)缓存机制。通过具体示例,展示了如何进行数据获取和写入操作,以及在缓存容量达到上限时如何自动淘汰最近最少使用的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。

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

进阶:

你是否可以在 O(1) 时间复杂度内完成这两种操作?

示例:

LRUCache cache = new LRUCache( 2 /* 缓存容量 */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // 返回  1
cache.put(3, 3);    // 该操作会使得密钥 2 作废
cache.get(2);       // 返回 -1 (未找到)
cache.put(4, 4);    // 该操作会使得密钥 1 作废
cache.get(1);       // 返回 -1 (未找到)
cache.get(3);       // 返回  3
cache.get(4);       // 返回  4

思路
利用双向链表和map实现 map存储key对应的双向链表中的节点 双向链表用来实现LRU
1. get操作 需要将map 中key对应的节点移动到头部位置
2. put操作 如果key存在 更新value 并将map 中key对应的节点移动到头部位置 ,不存在的话需要判断是否超出容量 如果超出, 则删除尾部节点并
将新节点插到头部位置,没有超出 则直接插到头部位置
实现
type LRUCache struct {
	Head *LRUCacheNode
	Last *LRUCacheNode
	Cap  int
	Map  map[int]*LRUCacheNode
}

type LRUCacheNode struct {
	Key   int
	Value int
	Pre   *LRUCacheNode
	Next  *LRUCacheNode
}

func Constructor(capacity int) LRUCache {					// 设置容量
	cache := LRUCache{}
	cache.Cap = capacity
	cache.Map = make(map[int]*LRUCacheNode)

	return cache
}

func (this *LRUCache) Get(key int) int {
	if node, ok := this.Map[key]; ok {
		if node == this.Last && node != this.Head {			// 如果需要移动的节点是最后一个且不是第一个 更新this.Last指针
			this.Last = node.Pre
		}
		this.Head = DoubleLinkMoveToHead(node, this.Head)	// 将节点移动到头位置
		return node.Value
	} else {
		return -1
	}
}

func (this *LRUCache) Put(key int, value int) {
	if node, ok := this.Map[key]; ok {
		node.Value = value									// 更新key对应的value
		if node == this.Last && node != this.Head {			// 如果需要移动的节点是最后一个且不是第一个 更新this.Last指针
			this.Last = node.Pre
		}
		this.Head = DoubleLinkMoveToHead(node, this.Head)	// 将节点移动到头位置
		return
	} else if len(this.Map) >= this.Cap {					// 如果超出容量 则删除最后一个节点 并删除map对应的key
		lastKey := this.Last.Key
		last := this.Map[lastKey]
		this.Last = last.Pre
		DoubleLinkDelete(last)
		delete(this.Map, lastKey)
	}

	this.Head = DoubleLinkInsert(this.Head, key, value)		// 删除后 插入新节点到头部位置

	this.Map[key] = this.Head								// 更新新节点的位置到map

	if len(this.Map) == 1 {									// 如果当前只有一个节点 则last 和head 指向同一位置
		this.Last = this.Head
	}

	return
}

func DoubleLinkInsert(head *LRUCacheNode, key, value int) *LRUCacheNode {	// 插入新节点
	newNode := new(LRUCacheNode)
	newNode.Key = key
	newNode.Value = value
	if head == nil {
		return newNode
	} else {
		newNode.Next = head
		head.Pre = newNode
		return newNode
	}
}

func DoubleLinkDelete(node *LRUCacheNode) {							// 删除指定节点
	pre := node.Pre
	next := node.Next

	if pre != nil {
		pre.Next = next
	}

	if next != nil {
		next.Pre = pre
	}

	node = nil
}

func DoubleLinkMoveToHead(node *LRUCacheNode, head *LRUCacheNode) *LRUCacheNode {	// 移动指定节点到头部位置
	if node != head {
		DoubleLinkDelete(node)
		node.Next = head
		node.Pre = nil
		head.Pre = node
		return node
	}

	return head
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值