• 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     }
  • 相关阅读:
    php-管理变量
    php-变量的间接引用
    php-eval()
    HTML
    php观
    笔记1
    脚本语言
    Windows Server 2012如何把快捷方式加到启动文件夹中
    VIM的笔记
    mongodb 从3.0 升级到3.2
  • 原文地址:https://www.cnblogs.com/asenyang/p/10485437.html
Copyright © 2020-2023  润新知