HOT35-LRU缓存

       leetcode原题链接LRU缓存

       上一篇HOT34-合并K个升序链表

       下一篇HOT36-二叉树的中序遍历

题目描述

       请你设计并实现一个满足  LRU (最近最少使用) 缓存 约束的数据结构。

实现 LRUCache 类:

  • LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存
  • int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
  • void put(int key, int value) 如果关键字 key 已经存在,则变更其数据值 value ;如果不存在,则向缓存中插入该组 key-value 。如果插入操作导致关键字数量超过 capacity ,则应该 逐出 最久未使用的关键字。

函数 get 和 put 必须以 O(1) 的平均时间复杂度运行。

示例:

输入
["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

解题方法:用list + map实现。list保存<key, value> pair, map保存<key, list::iter>。put和get分别要注意以下细节问题:

==put函数==

1.当前put的<key,value>中的key如果已经在map中,则只需要将list中的<key, value>挪到list的首部,同时更新map中该key对应的value。

2. 如果put的<key,value>中的key不在map中,则需要考虑:

     (1)list是否达到了最大容量,如果已经达到最大容量,需要先删除list的最后一个元素;

     (2)从list的首部插入<key,value>[更新list],同时将<key, list.begin()>更新到map中[更新map]。

 ==get函数==

1. 如果待查询的key不在map中,则直接返回-1;

2. 如果待查询的key在map中,则:

    (1)将该key对应的list中的节点从当前查询的位置挪到list的头部。(更新list)

    (2)更新map中key对应的值为list的begin()。(更新map)

    (3)返回key对应的value。(返回值)

3. 对应c++,这里会频繁的用到三个list的四个函数,分别是:

      list::back() -->获取list的最后一个元素的值

      list::push_front() -->从list的头部插入元素

      list::pop_back() -->从list的尾部删除元素

      list::splice(iter1, list2, iter2)-->将list2的iter2的元素挪到list的iter1的位置,如:lst.splice(lst.begin(), lst, iter)表示将lst的iter对应的元素移到list的头部。

C++代码

#include <map>
#include <list>
class LRUCache {
public:
    LRUCache(int capacity) {
        m_capacity = capacity;
    }
    int get(int key);//查询
    void put(int key, int value);//插入
private:
    int m_capacity;
    using NodeType = std::pair<int, int>;//list node保存的是(key,value)pair
    using ListIter = std::list<NodeType>::iterator;
    using MapIter = std::map<int, ListIter>::iterator;
    std::list<NodeType> m_list;//利用list实现o(1)删除和插入
    std::map<int, ListIter> m_mp;//利用map实现o(1)查询
};
int LRUCache::get(int key) {
    if (!m_mp.count(key)) { //元素不存在
        return -1;
    }
    ListIter list_iter = m_mp[key];
    m_list.splice(m_list.begin(), m_list, list_iter);//将结点移动到链表的头部
    m_mp[key] = m_list.begin();//更新map中key对应的value
    return m_mp[key]->second;
}

void LRUCache::put(int key, int value) {
    if (m_mp.count(key)) { //待插入的元素存在
        // 1. 更新key对应的value值
        ListIter p_node = m_mp[key];
        if (p_node != m_list.end()) {
            p_node->second = value;
        }
        // 2. 将<key, value>移到list的首位
        m_list.splice(m_list.begin(), m_list, m_mp[key]);
        // 3. 更新mp中key对应的位置
        m_mp[key] = m_list.begin();
        return;
    } 
    // 待插入的key不存在
    int n = m_list.size();
    if (n == m_capacity) { //当前元素个数满了
        // 1.先将list尾部的元素汰换
        m_mp.erase(m_list.back().first);
        m_list.pop_back();
        
    }
    // 2.在list头部插入新元素
    m_list.push_front({key, value});
    // 3.更新map的位置
    m_mp[key] = m_list.begin();
}
/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache* obj = new LRUCache(capacity);
 * int param_1 = obj->get(key);
 * obj->put(key,value);
 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值