leetcode LRU Cache

题目链接。实现一个数据结构用于LRU,最近最少使用,O(1)插入和删除。关于LRU的基本知识可参考here

先推荐JustDoIT的。

下面是我自己实现的。

class LRUCache{
public:
//146LRU Least Recently Used
    int LRUsize;
    struct LRUNode
    {
        int key;
        int value;
        LRUNode *pre, *next;
        LRUNode(int x, int y): key(x), value(y), pre(NULL), next(NULL){}
    };
    unordered_map<int, LRUNode *> LRUmap;
    LRUNode *head = NULL, *tail = NULL;

    LRUCache(int capacity)
    {
        LRUsize = capacity;
    }

    int get(int key)
    {
        if (LRUmap.count(key)) // if exists
        {
            if (head != LRUmap[key] && tail != LRUmap[key]) // 节点在中间
            {
                LRUmap[key] -> pre -> next = LRUmap[key] -> next;
                LRUmap[key] -> next -> pre = LRUmap[key] -> pre;
                LRUmap[key] -> next = head;
                head -> pre = LRUmap[key];
                head = LRUmap[key];
                head -> pre = NULL;
            }
            else if (head != LRUmap[key] && tail == LRUmap[key]) // 节点最后一个则放在头部即可
            {
                tail = LRUmap[key] -> pre;
                tail -> next = NULL;
                LRUmap[key] -> pre = NULL;
                head -> pre = LRUmap[key];
                LRUmap[key] -> next = head;
                head = LRUmap[key];
            }
            return LRUmap[key] -> value;
        }
        else
            return -1;
    }

    void set(int key, int value)
    {
        if (LRUmap.count(key))
        {
            LRUmap[key] -> value = value; // 一定要更新value
            if (head != LRUmap[key] && tail != LRUmap[key]) // 节点在中间
            {
                LRUmap[key] -> pre -> next = LRUmap[key] -> next;
                LRUmap[key] -> next -> pre = LRUmap[key] -> pre;
                LRUmap[key] -> next = head;
                head -> pre = LRUmap[key];
                head = LRUmap[key];
                head -> pre = NULL;
            }
            else if (head != LRUmap[key] && tail == LRUmap[key]) // 节点最后一个则放在头部即可
            {
                tail = tail -> pre;
                tail -> next = NULL;
                LRUmap[key] -> pre = NULL;
                head -> pre = LRUmap[key];
                LRUmap[key] -> next = head;
                head = LRUmap[key];
            }
        }
        else
        {
            LRUNode *tmp = new LRUNode(key, value);
            if (head == tail && head == NULL)
            {
                head = tmp;
                tail = tmp;
                LRUmap[key] = head;

            }
            else if (LRUsize == LRUmap.size()) //注意可能容量只为1,满了记得删除
            {
                tmp -> next = head;
                head -> pre = tmp;
                head = tmp;
                LRUmap[key] = tmp;
                LRUmap.erase(tail -> key);
                tail = tail -> pre;
                tail -> next = NULL;

            }
            else
            {
                tmp -> next = head;
                head -> pre = tmp;
                head = tmp;
                LRUmap[key] = tmp;
            }
        }
    }
};

虽然AC了,不过我还在纠结不知道如何delete代码中new出来的内存。如果用java的话就不用delete了。但是看所有的leetcode提交的分布,c++普遍是速度最快的。

貌似给出推荐的JustDOIT的也是没有delete的。

然后我就找啊找。

这一篇写的还不错。里面有原理解释,而且delete在析构中进行。用可用节点的数组存。或者是用原有的已经有的尾部来存新进入的点。这个想法不错。

这题还是考察挺多的。出现频率高,有时间还是要好好消化。

时间: 2024-08-08 18:45:44

leetcode LRU Cache的相关文章

[leetcode]LRU Cache (python)

LRU:最近最久未使用,为了得到这个最新最久的信息,需要一种策略来进行记录,如果加入类似时间戳式的字段,那么每次删除的时候,就必须通过遍历才能得到时间信息,或者对时间戳进行排序,但是无论哪种,都是需要额外的维护,维护成本都比较高. 广泛使用的策略是底层用双端队列来进行维护,双端使得在插入删除时操作更简单.而单单使用双端队列似乎还是不够,比如在get 时,还是需要顺序查找给定的key参数的,所以为了能在O(1) 时间获得key 需要类hash的结构,在python里,就用字典. 接下来的事情是,我

LeetCode: LRU Cache [146]

[题目] 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

[leetcode]LRU Cache @ Python

原题地址:http://oj.leetcode.com/problems/lru-cache/ 题意:设计LRU Cache 参考文献:http://blog.csdn.net/hexinuaa/article/details/6630384 这篇博文总结的很到位.   https://github.com/Linzertorte/LeetCode-in-Python/blob/master/LRUCache.py 代码参考的github人写的,思路非常清晰,写的也很好. Cache简介: Ca

[LeetCode]LRU Cache, 解题报告

题目 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.

[LeetCode]LRU Cache有个问题,求大神解答

题目: 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.

LeetCode——LRU Cache

Description: 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 r

[LeetCode] LRU Cache 缓存器

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

LeetCode – LRU Cache (Java)

Problem 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

leetcode LRU Cache Golang

package main import( "fmt" ) type Node struct { Key string Val string Pre *Node Next *Node } type DLinkedList struct { Head *Node Tail *Node } func (self *DLinkedList) IsEmpty() bool { if self.Head == nil && self.Tail == nil { return tru