LeetCode -- LRU Cache

原创 2015年11月21日 10:15:44
题目描述:
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缓存,可以概括为一个指定容量的的缓存,当超出容量时,拿掉最长时间没有使用的那个元素。

关于LRU的具体定义可以查看这个链接:

http://mcicpc.cs.atu.edu/archives/2012/mcpc2012/lru/lru.html



思路:
1. 使用哈希来存[key,value]
2. 维护一个集合,根据key的使用情况(最早使用的放最后)更新位置


实现代码:


public class LRUCache {


    private Dictionary<int, int> _cache; 
	private List<int> _usedKeys;
	
	private int _capacity;
	
    public LRUCache(int capacity)
	{
		_cache = new Dictionary<int, int>();
		_usedKeys = new List<int>();
		
        _capacity = capacity;
    }


    public int Get(int key)
	{
        if(!_cache.ContainsKey(key)){
			return -1;
		}
		else{
			_usedKeys.Remove(key);
			_usedKeys.Insert(0, key);
			
			return _cache[key];
		}
    }


    public void Set(int key, int value) 
	{
		if(_cache.ContainsKey(key)){
			_cache[key] = value;
			
			_usedKeys.Remove(key);
			_usedKeys.Insert(0, key);
		}
		else{
			if(_cache.Keys.Count < _capacity){
				_cache.Add(key, value);
				
				_usedKeys.Insert(0, key);
			}
			else
			{
				var removing = _usedKeys.Last();
				_usedKeys.RemoveAt(_usedKeys.Count - 1);
				
				_cache.Remove(removing);
				_cache.Add(key, value);
				
				_usedKeys.Insert(0, key);
			}
		}
    }
    
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

【LeetCode】LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol...

LeetCode 6 LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache.

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

LeetCode LRU Cache

LeetCode LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. I...

Leetcode LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol...

Leetcode: LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol...

LRU Cache [leetcode]

使用双向链表+map,实现O(1)时间内的get和set 需要注意的是: 1. set时更新tail size为0时更新头部 size为capacity时删除头部并且更新头部 2. get时...

[LeetCode] LRU Cache

Question: Design and implement a data structure for Least Recently Used (LRU) cache. It should supp...

Leetcode: LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol...

[LeetCode] LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol...

LeetCode LRU Cache

题意:模拟实现LRU缓存,可以支持的功能有get(key)和set(key,value) 解法: 1、先来一个朴素的,自己模拟的双端队列,可惜TLE了,不过大家可以打打玩玩 public class...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)