【LeetCode】LRU Cache

题目:


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.


主题思路:

    使用链表保存数据,每次get()或者set()时,将对应的数据结点移到链表头部,以表示该数据是最近一次被访问的数据。为了更快速查找key对应的数据是否存在以及所在位置,每次插入新的数据结点时,使用map建立key与list迭代器的映射.


C++代码实现:

class LRUCache{
public:
    LRUCache(int cap):
	capacity_(cap)
    {
    }
    
    int get(int key) {  	
        map_iterator it = cache_map_.find(key);
	if (it == cache_map_.end() || 0 == capacity_) { 
	    return -1;
	} else { 
	    // 将访问的结点移到链表头部 
	    cache_list_.splice(cache_list_.begin(), cache_list_, it->second);
	    return cache_list_.begin()->value;
        }
    }
    
    void set(int key, int value) {			
        map_iterator it = cache_map_.find(key);
        if (it != cache_map_.end()) { // 如果结点已经存在 
            // 将访问的结点移到链表头部
            // 然后更新value 
            cache_list_.splice(cache_list_.begin(), cache_list_, it->second);
            cache_list_.begin()->value = value;
        } else { // 否则新建插入 
            // 容量已满,移除末节点 
            if (cache_list_.size() == capacity_) { 
                cache_map_.erase(cache_list_.back().key);
                cache_list_.erase(--cache_list_.end());
            }	
            // 插入新的结点	
            cache_list_.push_front(ListNode(key,value)); 
            cache_map_[key] = cache_list_.begin();
        }
    }

private:
    struct ListNode {
        int       key;
        int       value;
        ListNode(int _key, int _value): key(_key), value(_value) {
        }
    };
	
    typedef list<ListNode>::iterator             list_iterator;
    typedef map<int, list_iterator>::iterator    map_iterator;
	
    int                     capacity_;
    list<ListNode>          cache_list_;
    map<int, list_iterator> cache_map_;
};


     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值