leetcode146
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的更多相关文章
- [Swift]LeetCode146. LRU缓存机制 | LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- LeetCode146:LRU Cache
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- 【Leetcode146】LRU Cache
问题描述: 设计一个LRU Cache . LRU cache 有两个操作函数. 1.get(key). 返回cache 中的key对应的 val 值: 2.set(key, value). 用伪代码 ...
- leetcode146周赛-1131-绝对值表达式的最大值
题目描述: class Solution: def maxAbsValExpr(self, arr1, arr2) -> int: def function(s1,s2): result1=[] ...
- leetcode146周赛-1130-叶值的最小代价生成树*
题目描述: class Solution(object): def mctFromLeafValues(self, arr): """ :type arr: List[i ...
- leetcode146周赛-5132-颜色最短的交替路径
---恢复内容开始--- 题目描述: class Solution: def shortestAlternatingPaths(self, n: int, red_edges, blue_edges) ...
- leetcode146周赛-5130-等价多米诺骨牌对的数量
题目描述: 方法一: class Solution(object): def numEquivDominoPairs(self, dominoes): """ :type ...
- 操作系统-2-存储管理之LRU页面置换算法(LeetCode146)
LRU缓存机制 题目:运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制. 它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - ...
- leetcode146 longest-substring-without-repeating-character
题目描述 给定一个字符串,找出最长的不具有重复字符的子串的长度.例如,"abcabcbb"不具有重复字符的最长子串是"abc",长度为3.对于"bbb ...
随机推荐
- React项目新手指南
对于程序员而言:驼峰和下划线之间是一场宗派战争:大括号是否换行会成为一种党派:逗号写在行尾还是行首的人来自不同星球…… 然而,无规矩不成方圆,任何一个团队,要想有高质量的产出,第一步必须要对一些基本的 ...
- Java语法基础学习DayFifteen(IO续)
一.缓冲流(处理流的一种) 1.作用:可以提高文件操作的效率 2.使用BufferedInputStream和BufferedOutputStream实现非文本文件的复制 特点:flush()方法 代 ...
- IIS 8.5详细错误
把网站部署到IIS后报错,我错的原因是文件夹内没有设置默认文档,计算机不知道运行哪一个文件,所以报错. 方法:(1)在IIS目录下找到默认文档,双击,点击添加,手写 自己的 启动文件 (2)启动自己的 ...
- noj快排
1003.快速排序 时限:1000ms 内存限制:10000K 总时限:3000ms 描述 给定一个数列,用快速排序算法把它排成升序. 输入 第一行是一个整数n(n不大于10000),表示要排序 ...
- Bitmap每个像素值由指定的掩码决定
bBitmap每个像素值由指定的掩码决定 写了一个小工具来转换Bitmap彩色图片的不同位数. 程序的逻辑是拼接: Bitmap文件头 + Bitmap信息头 + Bitmap数据 在RGB888转换 ...
- 强大的拖拽组件:React DnD 的使用
强大的拖拽组件:React DnD 的使用 react.js 10.6k 次阅读 · 读完需要 25 分钟 17 文章首发我的个人blog : 原文链接 学习 React DnD 的最初原因是阅读 ...
- PythonStudy——内存管理之垃圾回收 Memory management Garbage collection
内存管理 引用计数:垃圾回收机制的依据 # 1.变量的值被引用,该值的引用计数 +1# 2.变量的值被解绑,该值的引用计数 -1# 3.引用计数为0时就会被垃圾回收机制回收 标记清除:解决循环引用问题 ...
- yuan先生博客链接
Yuan先生的博客网址 1 Web应用 https://www.cnblogs.com/yuanchenqi/articles/8869302.html 2 http协议 https://www ...
- 第0章 概述及常见dos命令
计算机发展史 计算机的发展历史有多长?真正意义上的计算机诞生,距今也只有80多年的时间.80年,对于每一个人来说,是很长的时间,但对于整个历史来说,只是短短的一瞬间. 从第一代电子计算机的发明,到今天 ...
- Written a lua threadpool
工作原理 由于lua只能单线程运行,该lib要求所有lua代码在单线程,而多线程部分只能为c代码 具体用法上要求多线程部分必须用c实现 相关模块 线程池 异步函数实现框架 Now only a sle ...