前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode 146 LRU Cache 模拟操作系统LRU

Leetcode 146 LRU Cache 模拟操作系统LRU

作者头像
triplebee
发布2018-01-12 14:50:28
6100
发布2018-01-12 14:50:28
举报

科研学习压力比较大,最近更新的不多,有时间尽量多做一点吧!上来就是一个大模拟。

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

模拟实现一个LRU,LRU是操作系统里的一个算法,当存储空间满的时候,需要换掉使用时间离现在最远的数据。

分析:有一个内存容量 size,通过构造函数赋予大小

get操作,获取指定键的值,没有则返回-1,如果获取成功相当于成功访问了数据,需要将它提到最前面。

get不会改变数据的多少,所以不会出现超过容量size的情况。

set操作分三种情况:1.找到key,那么修改value值,将它提到前面就行了。

2. 没有找到key,当前容量未满,创建key-value对,把它放在最前面。

3. 没有找到key,当前容量已满,创建key-value对,把它放在最前面,把最后面的移除。

代码如下,详见注释:

代码语言:javascript
复制
class LRUCache{
public:
    LRUCache(int capacity) //初始化容量
    {
        size = capacity;
    }
    
    int get(int key) 
    {
        if(mp.find(key) != mp.end()) //get到了
        {
            node* p = mp[key];
            node* q = mp[key]->next;
            p->next = q->next;
            if(q->next) 
                mp[q->next->k] = p;
            else
                tail = p;
            q->next = head->next;
            if(head->next) 
                mp[head->next->k] = q;
            else
                tail = q;
            head->next = q;
            mp[q->k] = head;
            return mp[key]->next->val;
        }
        return -1; //没有get到
    }
    
    void set(int key, int value) 
    {
        if(mp.find(key) != mp.end()) //有这个key
        {
            node* p = mp[key];
            node* q = mp[key]->next;
            p->next = q->next;
            if(q->next) 
                mp[q->next->k] = p;
            else
                tail = p;
            q->next = head->next;
            if(head->next) 
                mp[head->next->k] = q;
            else
                tail = q;
            head->next = q;
            mp[q->k] = head;
            q->val = value;
            return ;
        }
        if(now < size) //没有key,但是还有容量
        {
            now++;
            node* p = new node(key, value);
            mp[key] = head;
            if(head->next) mp[head->next->k] = p;
            
            p->next = head->next; 
            head->next = p;
            if(!p->next) tail = p;
            return ;
        }
        if(tail) //没有key,但没有容量了,有tail说明容量不为0,可以装入新键值对
        {
            node* p = new node(key, value);
            mp[key] = head;
            if(head->next) mp[head->next->k] = p;
            p->next = head->next; 
            head->next = p;
            tail = mp[tail->k];
            mp.erase(tail->next->k);
            delete tail->next;
            tail->next = NULL;
        }
    }
    
private:
    struct node 
    {
        int k,val;
        node* next;
        node(int a, int b)
        {
            k = a;
            val = b;
            next = NULL;
        }
    };
    node* head = new node(-1, -1);
    node* tail;
    int size, now = 0; //最大容量和当前容量
    unordered_map<int, node*> mp; //键对应的链表节点的前驱
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-12-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

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