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(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.

思路:

问题的关键是实现一个优先队列。由于数组的时间开销太大,我选择了链表以及数列来实现这个优先队列。当然,如果需要更好的性能,可以考虑使用堆栈来实现。

代码:

链表版本

class ListNode1{
public:
    int key;
    int value;
    ListNode1* next;
    ListNode1(int key, int value)
    {
        this->key = key;
        this->value = value;
        next = NULL;
    }
};

class LRUCache{
public:
    ListNode1 * begin;
    int len;
    LRUCache(int capacity) {
        begin = NULL;
        len = capacity;
    }
    
    int get(int key) {
        if(begin==NULL)
            return -1;
        if(begin->key == key)
        {
            return begin->value;
        }
        else
        {
            ListNode1* p = begin;
            int cur = 1;
            while(p->next != NULL&&cur < len)
            {
                cur++;
                if(p->next->key == key)
                {
                    ListNode1* tmp = p->next;
                    p->next = p->next->next;
                    tmp->next = begin;
                    begin = tmp;
                    return begin->value;
                }
                p=p->next;
            };
            return -1;
        }
    }
    
    void set(int key, int value) {
        if(begin == NULL)
            begin =new ListNode1(key,value);
        else
        {
            ListNode1* p = new ListNode1(key,value);
            p->next= begin;
            begin=p;
            int cur = 1;
            while(p->next != NULL&&cur < len)
            {
                cur++;
                if(p->next->key == key)
                {
                    p->next = p->next->next;
                    cur--;
                    continue;
                }
                p=p->next;
            };
            p->next=NULL;
        }
    }
};

数列版本

class KeyValuePair{
public:
    int key;
    int value;
};

class LRUCache{
public:
    KeyValuePair* cache;
    int length;
    int capacity;
    LRUCache(int capacity) {
        cache = new KeyValuePair[capacity];
        length = 0;
        this->capacity = capacity;
    }
    
    int get(int key) {
        int value = searchAndGetValue(key);
        
        if(value != -1){
            insert(key, value);
        }
        
        return value;
    }
    
    void set(int key, int value) {
        searchAndGetValue(key);
        
        if(length == capacity){
            remove(0);
        }
        
        insert(key, value);
    }
    
    int searchAndGetValue(int key){
        int result = -1;
        for(int i = 0; i < length; i++){
            if(cache[i].key == key){
                result = cache[i].value;
                remove(i);
                break;
            }
        }
        
        return result;
    }
    
    void insert(int key, int value){
        KeyValuePair tmp;
        tmp.key = key;
        tmp.value = value;
        cache[length++] = tmp;
    }
    
    void remove(int index){
        for(int i = index; i < length - 1; i++){
            cache[i] = cache[i+1];
        }
        length--;
    }
};
    原文作者:Allanxl
    原文地址: https://blog.csdn.net/lanxu_yy/article/details/17328333
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞