leetcode 146. LRU缓存机制 medium (重要)

25 篇文章 0 订阅
19 篇文章 0 订阅

leetcode 146. LRU缓存机制   medium  (重要)       

题目描述:

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

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

进阶:

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

解题思路:

LRU 是 Least Recently Used 的简写,就是最近最少使用的意思。那么这个缓存器主要有两个成员函数,get 和 put,都需要在O(1) 时间内实现。

  1. get 函数是通过输入 key 来获得 value,如果成功获得后,这对 (key, value) 升至缓存器中最常用的位置(顶部),如果 key 不存在,则返回 -1。
  2. 而 put 函数是插入一对新的 (key, value),如果原缓存器中有该 key,则需要先删除掉原有的,将新的插入到缓存器的顶部。如果不存在,则直接插入到顶部,并且假如加入新的值后缓存器超过了容量,则需要先删掉一个最不常用的值,也就是底部的值。

所以首先我们需要一个双向链表,链表里元素的顺序表示距离当前访问的顺序,链表头是最近刚被访问的,链表尾是最近最远被访问的,并且我们为了在O(1)时间内实现get函数,我们还需要一个hash表。为了方便,我们hash表里存的时key和list迭代器的映射

并且用li.splice(li.begin(),li,hash[key]); // 把i所指元素放到li.begin 之前   这个来调整list位置

代码:

go 代码

type listNode struct {
	key  int
	val  int
	prev *listNode
	next *listNode
}

type LRUCache struct {
	capacity int
	mp       map[int]*listNode
	head     *listNode
}

func Constructor(capacity int) LRUCache {
	head := &listNode{}
	head.prev = head
	head.next = head
	return LRUCache{
		capacity: capacity,
		mp:       map[int]*listNode{},
		head:     head,
	}

}

func (this *LRUCache) Get(key int) int {
	node := this.get(key)
	if node == nil {
		return -1
	}
	return node.val
}

func (this *LRUCache) Put(key int, value int) {
	node := this.get(key)
	if node != nil {
		node.val = value
		return
	}
	if len(this.mp) == this.capacity {
		node = this.head.prev
		node.prev.next = node.next
		node.next.prev = node.prev
		delete(this.mp, node.key)
	}

	node = &listNode{
		key:  key,
		val:  value,
		prev: this.head,
		next: this.head.next,
	}
	node.prev.next = node
	node.next.prev = node
	this.mp[key] = node
}

func (this *LRUCache) get(key int) *listNode {
	node, ok := this.mp[key]
	if !ok {
		return nil
	}
	node.prev.next = node.next
	node.next.prev = node.prev

	node.next = this.head.next
	node.prev = this.head
	node.prev.next = node
	node.next.prev = node

	return node
}
//
class LRUCache {
public:
    LRUCache(int capacity):cap(capacity) {
        
    }
    
    int get(int key) {
      if(hash.count(key)){
          li.splice(li.begin(),li,hash[key]); // 把key所指的那个元素给移到最前面
          return li.front().second;
        } 
        
      return -1;
    }
    
    void put(int key, int value) {
        if(hash.count(key)){
          li.splice(li.begin(),li,hash[key]); // 把i所指元素放到li.begin 之前
          li.front().second=value;
        } else{
          li.push_front({key,value});
          hash[key]=li.begin();
          if(li.size()>cap){
            hash.erase(li.back().first);
            li.pop_back();
          }
        }
          
    }
  
private:
  int cap;
  list<pair<int,int>> li; // 保存key-val 因为要pop_front
  unordered_map<int,list<pair<int,int>>::iterator> hash; // 保存key和迭代器的映射
  
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值