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     }

 

补充一个python的实现:

 1 import collections
 2 
 3 class LRUCache(collections.OrderedDict):
 4 
 5     def __init__(self, capacity: int):
 6         super().__init__()
 7         self.capacity = capacity
 8 
 9 
10     def get(self, key: int) -> int:
11         if key not in self:
12             return -1
13         self.move_to_end(key)
14         return self[key]
15 
16     def put(self, key: int, value: int) -> None:
17         if key in self:
18             self.move_to_end(key)
19         self[key] = value
20         if len(self) > self.capacity:
21             self.popitem(last=False)

 

posted on 2019-03-06 19:26  Sempron2800+  阅读(174)  评论(0编辑  收藏  举报