#Leet Code# LRU Cache

语言: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 = 0;
12
13         this->headNode = new Node();
14         this->headNode->key = -1;
15         this->headNode->value = -1;
16         this->headNode->next = NULL;
17
18         this->lastNode = this->headNode;
19     }
20
21     int get(int key) {
22         if (this->curLength == 0)
23             return -1;
24
25         Node* tmpNode = reSequencing(key, -1);
26         if (tmpNode != NULL) {
27             return tmpNode->value;
28         } else {
29             return -1;
30         }
31     }
32
33     void set(int key, int value) {
34         if (this->capacity == 0) return;
35
36         Node* tmpNode = reSequencing(key, value);
37         if (tmpNode != NULL) {
38             tmpNode->value = value;
39             return;
40         }
41
42         if (this->curLength + 1 > 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 += 1;
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

时间: 2024-10-18 20:46:13

#Leet Code# LRU Cache的相关文章

【Lintcode】LRU Cache, Data Stream Median

主要是priority_queue的用法 一个是内置类型优先队列怎么设置小根堆(默认大根堆) 如果是自定义数据结构,有两种办法 1.定义这种数据结构的比较符号,就可以当成内置类型整 2.传进去一个重载()的类,当小于号用,默认还是大根堆,也许传进去的是个callable object都行的吧,我试了一下函数好像不行,不懂,不管了 LRU Cache class LRUCache{ public: // @param capacity, an integer int Time; typedef i

8.12 [LeetCode] 146 LRU Cache

Question link Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise

leetcode || 146、LRU Cache

problem: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise retur

【LeetCode 题解】 LRU Cache

设计并实现最近最久未使用(Least Recently Used)缓存. 链接:https://oj.leetcode.com/problems/lru-cache/ 题目描述: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (wi

146. LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. put

LRU Cache的简单实现

Cache这个东西可以说无处不在,处理器中的TLB,Linux系统中的高速页缓存,还有很多人熟知的开源软件memcached,都是cache的一种实现.LRU是Least Recently Used的缩写,即最近最少使用,是常用cache算法中的一种.因为cache的存储空间相对于后端存储来说更有限,将cache空间和后端存储空间映射后,还需要一些算法来解决cache满的问题并保证效率,LRU就是在cache满了以后,将最近最少访问到的内容移除,然后将新的内容放入cache,新的内容也成为了最近

【LRU Cache】cpp

题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

LRU Cache (9)

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. set

[leetcode]LRU Cache (python)

LRU:最近最久未使用,为了得到这个最新最久的信息,需要一种策略来进行记录,如果加入类似时间戳式的字段,那么每次删除的时候,就必须通过遍历才能得到时间信息,或者对时间戳进行排序,但是无论哪种,都是需要额外的维护,维护成本都比较高. 广泛使用的策略是底层用双端队列来进行维护,双端使得在插入删除时操作更简单.而单单使用双端队列似乎还是不够,比如在get 时,还是需要顺序查找给定的key参数的,所以为了能在O(1) 时间获得key 需要类hash的结构,在python里,就用字典. 接下来的事情是,我