public class LRUCache
{
int Capacity = ;
int Curlen = ;
long sernumbers;
long SerNumbers
{
get
{
if (sernumbers <= long.MaxValue)
{
return sernumbers;
}
else
{
dic.Clear();
return ;
}
}
set
{
sernumbers = value;
} }
Dictionary<int, KeyValuePair<int, long>> dic = new Dictionary<int, KeyValuePair<int, long>>();
//外层的Key为LRU的key,
//内层的Key为LRU的Value,内层的Value为访问编号 public LRUCache(int capacity)
{
Capacity = capacity;
} public int Get(int key)
{
if (dic.ContainsKey(key))
{
var K = dic[key].Key;
dic[key] = new KeyValuePair<int, long>(K, SerNumbers++);
return K;
}
else
{
return -;
}
} public void Put(int key, int value)
{
if (dic.ContainsKey(key))
{
dic[key] = new KeyValuePair<int, long>(value, SerNumbers++);
}
else
{
if (Curlen < Capacity)
{
dic.Add(key, new KeyValuePair<int, long>(value, SerNumbers++));
Curlen++;
}
else
{
var evictkey = dic.OrderBy(x => x.Value.Value).FirstOrDefault().Key;
dic.Remove(evictkey);
dic.Add(key, new KeyValuePair<int, long>(value, SerNumbers++));
} }
}
}

经典题目LRU被从Hard降低为Medium类别了,原来的实现有一点问题,

我又找了一个使用C#封装的数据结构LinkedList的实现,写起来更加简单。

linkedlist,链头记录最旧的数据,链尾记录最新的数据。

 public class LRUCache
{ private readonly LinkedList<int> _queue;
private readonly Dictionary<int, int> _dict;
private readonly int _capacity; public LRUCache(int capacity)
{
_queue = new LinkedList<int>();
_dict = new Dictionary<int, int>();
_capacity = capacity;
} public void Put(int key, int value)
{
if (_dict.TryGetValue(key, out var previousValue))
{
_dict.Remove(key);
_queue.Remove(key);
} if (_queue.Count == _capacity)
{
var first = _queue.First;
_dict.Remove(first.Value);
_queue.RemoveFirst();
} _dict[key] = value;
_queue.AddLast(key);
} public int Get(int key)
{
if (!_dict.TryGetValue(key, out int value))
{
return -;
} _queue.Remove(key); _queue.AddLast(key); return value;
}
}

leetcode146的更多相关文章

  1. [Swift]LeetCode146. LRU缓存机制 | LRU Cache

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

  2. LeetCode146:LRU Cache

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

  3. 【Leetcode146】LRU Cache

    问题描述: 设计一个LRU Cache . LRU cache 有两个操作函数. 1.get(key). 返回cache 中的key对应的 val 值: 2.set(key, value). 用伪代码 ...

  4. leetcode146周赛-1131-绝对值表达式的最大值

    题目描述: class Solution: def maxAbsValExpr(self, arr1, arr2) -> int: def function(s1,s2): result1=[] ...

  5. leetcode146周赛-1130-叶值的最小代价生成树*

    题目描述: class Solution(object): def mctFromLeafValues(self, arr): """ :type arr: List[i ...

  6. leetcode146周赛-5132-颜色最短的交替路径

    ---恢复内容开始--- 题目描述: class Solution: def shortestAlternatingPaths(self, n: int, red_edges, blue_edges) ...

  7. leetcode146周赛-5130-等价多米诺骨牌对的数量

    题目描述: 方法一: class Solution(object): def numEquivDominoPairs(self, dominoes): """ :type ...

  8. 操作系统-2-存储管理之LRU页面置换算法(LeetCode146)

    LRU缓存机制 题目:运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制. 它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - ...

  9. leetcode146 longest-substring-without-repeating-character

    题目描述 给定一个字符串,找出最长的不具有重复字符的子串的长度.例如,"abcabcbb"不具有重复字符的最长子串是"abc",长度为3.对于"bbb ...

随机推荐

  1. 剑指Offer 16. 合并两个排序的链表 (链表)

    题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 题目地址 https://www.nowcoder.com/practice/d8b6b4358 ...

  2. 著名的Log4j是怎么来的?

    Java在设计之初,借鉴了很多其他语言不错的特性和优点,唯独没有设计日志系统,但是日志的重要性不言而喻,一旦程序运行起来,运行结果与预期不一致,基本就是出Bug了,这个时候需要进行Bug排查,一般有两 ...

  3. matplotlib画sin(x)和cos(x)/2

    import matplotlib.pyplot as mp 1. 基本的绘图 mp.plot(水平坐标, 垂直坐标, linestyle=线型, linewidth=线宽, color=颜色, .. ...

  4. selenium-java,selenium版本和火狐浏览器版本对应关系

    selenium3.5.0,firefox57,geckodriver-v0.19.1

  5. matlab学习(4) any 和cellfun用法

    1.对于向量来说,只要包含非0元素,则返回为1: 2.对于矩阵来说,any(X)依次判断X的每一列是否为ture,返回一个含0或1的向量. 3.any(X,DIM)对X的第DIM维操作, DIM=1即 ...

  6. linux基础之系统管理类命令

    系统管理类命令 1.reboot.halt.poweroff命令 基本介绍 reboot命令.halt命令.poweroff命令:都表示重启或者关闭系统 基本语法 reboot/halt/powero ...

  7. Jquery仿百度经验左右滚动切换效果(转)

    http://www.xwcms.net/webAnnexImages/fileAnnex/201608/61342/index.html

  8. bootstrap table表格前台分页,点击tab选项,重新刷新表格

    近期做项目的时候使用bootstrap表格前台分页,并且有一个tab切换选项,共用一个table,效果如下图,上方是tab选项,下方是table: 在实际实现的时候,在默认状态下,表格翻到了第5页,此 ...

  9. /etc/hosts和/etc/hostname区别

    /etc/hosts主要是ip和域名的对应 /etc/hostname主要是本地主机域名(本地主机名修改过后需要重启服务器才能生效) 如果我想在另一台linux主机里面使用域名访问上面这台主机A,只需 ...

  10. Python的深copy和浅copy

    浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象. 深拷贝(deepcopy): copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象. 浅copy: a = [1, 2, ...