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. 邮件远控电脑MCC-python实现

    本次实现的是一个通过邮件来远程控制电脑,以达到某些远程操作,例如让电脑执行CMD命令,播放音乐,打开指定文件等操作的项目.代码参考了网上的部分教程. 具体流程: 在python代码中,通过一个循环来接 ...

  2. 关于idea的debug

    idea的debug真的是超级好用哎.分享几个今天学会的新方式: 1.右键会发现此选项 ,点击出现 在输入框中输入,可以通过某些公式单独计算. 2.点击属性值,右键点击set values 会出现一个 ...

  3. zabbix客户端的安装、zabbix主被动模式、添加主机模板等、处理页面的中文乱码

    1.zabbix客户端的安装: 如下步骤: wget repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch. ...

  4. 《DSP using MATLAB》Problem 7.6

    代码: 子函数ampl_res function [Hr,w,P,L] = ampl_res(h); % % function [Hr,w,P,L] = Ampl_res(h) % Computes ...

  5. 学习笔记TF050:TensorFlow源代码解析

    TensorFlow目录结构. ACKNOWLEDGMENTS #TensorFlow版本声明 ADOPTERS.md #使用TensorFlow的人员或组织列表 AUTHORS #TensorFlo ...

  6. python------Socket网络编程(二)粘包问题

    一.socket网络编程 粘包:服务端两次发送指令在一起,它会把两次发送内容合在一起发送,称为粘包,从而出现错误. 解决方法:(比较low的方法) 有些需要实时更新的,用sleep有延迟,不能这样解决 ...

  7. AJAX 请求后使用 JS 打开新标签页被阻止的解决方法

    需求:发起一个 AJAX 请求,根据请求结果来打开一个新页面. 问题:AJAX 请求后,使用 window.open() 方法来打开新页面会被浏览器阻止. 解决方法:在 AJAX 请求之前,就使用 c ...

  8. LeetCode - Unique Email Addresses

    Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...

  9. 全志A33 lichee 开发板 Linux中断编程原理说明

    开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 本节实验目标实现按键触发中断 ...

  10. 为git服务器配置gitosis管理权限

    yum install python-setuptools git clone https://github.com/tv42/gitosis.git cd gitosis sudo python s ...