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. 添加网络打印机的步骤(xp和win2008+win7)

    1.如题,设置好打印机的 ip地址和子网掩码等信息. 2 .xp不像其他新的系统那么好用那么智能...只能慢慢来 如果是xp,注意,请添加网络打印机的时候选  :添加本地打印机,,记得哦 然后如图 然 ...

  2. postman接口测试基础知识

    接口文档访问地址:http://doc.nnzhp.cn/index.php?s=/6&page_id=8 1.什么是接口:从数据库里面取数据或是插入数据 接口测试:测接口一定要有接口文档-- ...

  3. Layer For Mobile 弹窗 input输入文字后,点击取消确定按钮失效(需点击两次)

    webapp中使用Layer For Mobile弹出弹窗,修改昵称输入文字后,ios手机中,如果不先点击收起键盘,两个按钮点击之后无效... 两个按钮的方法是写在这里的——> 最后只能吧点击按 ...

  4. Hadoop学习笔记05_HA

    ################# HA 即 High Available 高可用.# 其作用是为了减少主从结构的单点故障,而设置备用节点,既然学习了Hadoop生态圈,那么HA配置也是必须要掌握的. ...

  5. TensorFlow 算术运算符

    TensorFlow 算术运算符 TensorFlow 提供了几种操作,您可以使用它们将基本算术运算符添加到图形中. tf.add tf.subtract tf.multiply tf.scalar_ ...

  6. vue与mapbox

    1.引入mapbox 在index.html里面引入maobox gl 或者使用 npm install mapbox-gl 2.功能 我主要这次要实现 地图展示 地图打点(添加Marker) 地图的 ...

  7. Apple IAP Subscriptions

    Apple Doc: https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/ ...

  8. ReactiveCocoa基础

    在讲ReactiveCocoa之前,我们来回忆一下Block在开发中的使用场景: 1.把block保存到对象中,等到恰当的时候才去调用 2.把block当做方法的参数使用,外界不调用,都是方法内部去调 ...

  9. Dubbo的全局Filter配置

    前言: 之前也写过dubbo的filter的文章, 后来和同事也有过交流, 才发生自己对dubbo的filter的机制, 还是存在一些误解, 尤其是自定义filter的定位, 不是那么清晰. 本文主要 ...

  10. Pycharm自动添加文件头

    Pycharm自动添加文件头 在编程的时候,我们往往需要在文件头里添加一些编码和作者信息,在Pycharm中,系统给我们自带了这一功能,可以做如下设置: 打开设置 在设置中找到如下选项: 然后在编辑框 ...