leetcode146

 1 public class LRUCache
 2     {
 3         int Capacity = 0;
 4         int Curlen = 0;
 5         long sernumbers;
 6         long SerNumbers
 7         {
 8             get
 9             {
10                 if (sernumbers <= long.MaxValue)
11                 {
12                     return sernumbers;
13                 }
14                 else
15                 {
16                     dic.Clear();
17                     return 0;
18                 }
19             }
20             set
21             {
22                 sernumbers = value;
23             }
24 
25         }
26         Dictionary<int, KeyValuePair<int, long>> dic = new Dictionary<int, KeyValuePair<int, long>>();
27         //外层的Key为LRU的key,
28         //内层的Key为LRU的Value,内层的Value为访问编号
29 
30         public LRUCache(int capacity)
31         {
32             Capacity = capacity;
33         }
34 
35         public int Get(int key)
36         {
37             if (dic.ContainsKey(key))
38             {
39                 var K = dic[key].Key;
40                 dic[key] = new KeyValuePair<int, long>(K, SerNumbers++);
41                 return K;
42             }
43             else
44             {
45                 return -1;
46             }
47         }
48 
49         public void Put(int key, int value)
50         {
51             if (dic.ContainsKey(key))
52             {
53                 dic[key] = new KeyValuePair<int, long>(value, SerNumbers++);
54             }
55             else
56             {
57                 if (Curlen < Capacity)
58                 {
59                     dic.Add(key, new KeyValuePair<int, long>(value, SerNumbers++));
60                     Curlen++;
61                 }
62                 else
63                 {
64                     var evictkey = dic.OrderBy(x => x.Value.Value).FirstOrDefault().Key;
65                     dic.Remove(evictkey);
66                     dic.Add(key, new KeyValuePair<int, long>(value, SerNumbers++));
67                 }
68 
69             }
70         }
71     }

 

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

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

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

 1 public class LRUCache
 2     {
 3 
 4         private readonly LinkedList<int> _queue;
 5         private readonly Dictionary<int, int> _dict;
 6         private readonly int _capacity;
 7 
 8         public LRUCache(int capacity)
 9         {
10             _queue = new LinkedList<int>();
11             _dict = new Dictionary<int, int>();
12             _capacity = capacity;
13         }
14 
15         public void Put(int key, int value)
16         {
17             if (_dict.TryGetValue(key, out var previousValue))
18             {
19                 _dict.Remove(key);
20                 _queue.Remove(key);
21             }
22 
23             if (_queue.Count == _capacity)
24             {
25                 var first = _queue.First;
26                 _dict.Remove(first.Value);
27                 _queue.RemoveFirst();
28             }
29 
30             _dict[key] = value;
31             _queue.AddLast(key);
32         }
33 
34         public int Get(int key)
35         {
36             if (!_dict.TryGetValue(key, out int value))
37             {
38                 return -1;
39             }
40 
41             _queue.Remove(key);
42 
43             _queue.AddLast(key);
44 
45             return value;
46         }
47     }

 

转载于:https://www.cnblogs.com/asenyang/p/10485437.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值