语言:C++

描述:使用单链表实现,HeadNode是key=-1,value=-1,next=NULL的结点。距离HeadNode近的结点是使用频度最小的Node。

  1. struct Node {
  2. int key;
  3. int value;
  4. Node* next;
  5. };
  6.  
  7. class LRUCache {
  8. public:
  9. LRUCache(int capacity) {
  10. this->capacity = capacity;
  11. this->curLength = ;
  12.  
  13. this->headNode = new Node();
  14. this->headNode->key = -;
  15. this->headNode->value = -;
  16. this->headNode->next = NULL;
  17.  
  18. this->lastNode = this->headNode;
  19. }
  20.  
  21. int get(int key) {
  22. if (this->curLength == )
  23. return -;
  24.  
  25. Node* tmpNode = reSequencing(key, -);
  26. if (tmpNode != NULL) {
  27. return tmpNode->value;
  28. } else {
  29. return -;
  30. }
  31. }
  32.  
  33. void set(int key, int value) {
  34. if (this->capacity == ) return;
  35.  
  36. Node* tmpNode = reSequencing(key, value);
  37. if (tmpNode != NULL) {
  38. tmpNode->value = value;
  39. return;
  40. }
  41.  
  42. if (this->curLength + > this->capacity) {
  43. if (this->headNode->next == this->lastNode) {
  44. delete this->lastNode;
  45. this->lastNode = this->headNode;
  46. } else {
  47. Node* t = this->headNode->next->next;
  48. delete this->headNode->next;
  49. this->headNode->next = t;
  50. }
  51. }
  52.  
  53. Node* newNode = new Node();
  54. newNode->key = key;
  55. newNode->value = value;
  56. newNode->next = NULL;
  57.  
  58. this->lastNode->next = newNode;
  59. this->lastNode = newNode;
  60.  
  61. curLength += ;
  62. }
  63.  
  64. Node* reSequencing(int key, int value) {
  65. Node* tmpNode = this->headNode;
  66. Node* preNode;
  67.  
  68. while (tmpNode != NULL) {
  69. if (tmpNode->key == key) {
  70. break;
  71. }
  72.  
  73. preNode = tmpNode;
  74. tmpNode = tmpNode->next;
  75. }
  76.  
  77. if (tmpNode != NULL && this->lastNode->key != key) {
  78. preNode->next = tmpNode->next;
  79.  
  80. this->lastNode->next = tmpNode;
  81. this->lastNode = tmpNode;
  82. tmpNode->next = NULL;
  83. }
  84.  
  85. return tmpNode;
  86. }
  87.  
  88. private:
  89. int capacity;
  90. int curLength;
  91.  
  92. Node* headNode;
  93. Node* lastNode;
  94. };

#Leet Code# LRU Cache的更多相关文章

  1. LeetCode之LRU Cache 最近最少使用算法 缓存设计

    设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...

  2. LRU Cache 题解

    题意 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  3. LeetCode题解: LRU Cache 缓存设计

    LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode   版权声明:本文为博主原创文章,遵循CC 4 ...

  4. Go LRU Cache 抛砖引玉

    目录 1. LRU Cache 2. container/list.go 2.1 list 数据结构 2.2 list 使用例子 3. transport.go connLRU 4. 结尾 正文 1. ...

  5. [LeetCode] LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  6. 【leetcode】LRU Cache

    题目简述: Design and implement a data structure for Least Recently Used (LRU) cache. It should support t ...

  7. LeetCode:LRU Cache

    题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...

  8. LRU Cache实现

    最近在看Leveldb源码,里面用到LRU(Least Recently Used)缓存,所以自己动手来实现一下.LRU Cache通常实现方式为Hash Map + Double Linked Li ...

  9. 【leetcode】LRU Cache(hard)★

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

随机推荐

  1. canvas绘制简单小铅笔

    对应HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...

  2. MyBatis+Spring 事务管理

                 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://kinglixing.blog.51cto.com/34 ...

  3. 【学习】ABAP OLE 对EXCEL的处理

    原文:http://blog.sina.com.cn/s/blog_7229b9c00100opx2.html -------------------------------------------- ...

  4. PowerMock注解PowerMockIgnore的使用方法

    故事要从一个异常开始,某天我在开发一个加密.解密特性,算法使用的是3DES,样例代码如下. package org.jackie.study.powermock; import java.io.Uns ...

  5. 【JavaScript设计模式系列---开篇预览】

    转:http://www.cnblogs.com/Darren_code/archive/2011/08/31/JavascripDesignPatterns.html 2011-08-31 23:5 ...

  6. A Cross-Platform Memory Leak Detector

    Memory leakage has been a permanent annoyance for C/C++ programmers. Under MSVC, one useful feature ...

  7. Java基础知识强化之IO流笔记22:FileInputStream / FileOutputStream 复制文本文件案例1

    1. 使用字节流FileInputStream / FileOutputStream 复制文本文件案例: 分析: (1)数据源:从哪里来 a.txt   --   读取数据  --  FileInpu ...

  8. ajax 基础教程

    这是一本什么书?这是一本技术类的书籍,主要从历史.XMLHttpRequest对象.怎么样于服务器交互.构建完备的Ajax开发工具箱.使用jsUnit测试javascript 代码,总之就是让我们从这 ...

  9. 自己做的demo---关于java控制台输入跟类型转化跟处理异常的demo

    package exception; import java.util.InputMismatchException; import java.util.Scanner; /*public class ...

  10. 使用linq语句获取指定条数的记录

    //获得指定个数的子文件夹,用于分页 var pageAlbums = (from SPFolder pf in lstSubAlbums select pf)                     ...