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(key, value) - Set or insert the value if the key is not
already present. When the cache reached its capacity, it should
invalidate the least recently used item before inserting a new item.

思路:Hashmap+double-linked list.

Hashmap: key是已给的key,value是ListNode的地址

double-linked list:当链表中某元素要被删除或提到head时,双向链表都方便进行操作

class LRUCache{
private:
/*double-linked list*/
struct ListNode{
ListNode * prev;
ListNode * next;
int key;
int value;
ListNode(int _key, int _value):key(_key), value(_value), prev(NULL), next(NULL){}
}; /*move the node to the head of liest*/
void change2TopPriority(ListNode* listNode){
if(head==listNode) return; //special case: it's the head
listNode->prev->next = listNode->next;
if(listNode->next) listNode->next->prev = listNode->prev;
else tail = tail->prev; //special case: it's the tail
listNode->prev = NULL;
listNode->next = head;
head->prev = listNode;
head = head->prev;
}
public:
LRUCache(int capacity) {
listNodeMap.clear();
this->capacity = capacity;
head = NULL;
tail = NULL;
} int get(int key) {
if(listNodeMap.find(key)!=listNodeMap.end()){
change2TopPriority(listNodeMap[key]);
return listNodeMap[key]->value;
}
else return -;
} void set(int key, int value) {
if(listNodeMap.find(key)==listNodeMap.end()){ // ket not exist
if(listNodeMap.size()==capacity){ // reach the capacity
//erase least used data
listNodeMap.erase(tail->key);
ListNode * tmp = tail;
if(head == tail) { //special case: capacity=1
head = tail = NULL;
}
else{
tail = tail->prev;
tail->next = NULL;
}
delete tmp;
}
//insert new node
ListNode * newNode = new ListNode(key,value);
if(!head){ //special case: first node in the list
head = tail = newNode;
}
else{
head->prev = newNode;
newNode->next = head;
head = head->prev;
}
listNodeMap[key]=newNode;
}
else{ //key already exists
listNodeMap[key]->value = value;
change2TopPriority(listNodeMap[key]);
}
}
private:
unordered_map<int,ListNode*> listNodeMap; //hashmap
ListNode* head; //head of double linked list
ListNode* tail; //tail of double linked list
int capacity; //capacity of CRU
};

146. LRU Cache (List, HashTable)的更多相关文章

  1. leetcode 146. LRU Cache 、460. LFU Cache

    LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...

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

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

  3. Java for LeetCode 146 LRU Cache 【HARD】

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

  4. leetcode 146. LRU Cache ----- java

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

  5. leetcode@ [146] LRU Cache (TreeMap)

    https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...

  6. 146. LRU Cache

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

  7. 【LeetCode】146. LRU Cache

    LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should suppo ...

  8. [LeetCode] 146. LRU Cache 近期最少使用缓存

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

  9. [LC] 146. LRU Cache

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

随机推荐

  1. Kanboard 看板工具配置使用

    备注:     类似的开源工具有wekan 界面还有功能和Trello 类似.比较方便   1. 安装(基于docker+ docker-compose) a. 安装docker && ...

  2. iOS App多语言国际化

    /*************************************1*******************************************/ /*************** ...

  3. Android源代码分析之拍照、图片、录音、视频和音频功能

    Android源代码分析之拍照.图片.录音.视频和音频功能   //选择图片 requestCode 返回的标识 Intent innerIntent = new Intent(Intent.ACTI ...

  4. [LeetCode系列]最大容器问题

    给定n个数: a1, a2, ... , an. 代表着(i, ai)个点, 连接这些点与对应的(i, 0), 我们可以得到n条线. 请在这n条线中找出2条, 使得这两条线和x轴构成的容器能够容纳最多 ...

  5. Eclipse的maven工具

    左侧是组件以及组件依赖树(层级结构):右侧是识别出来的所有的组件:  

  6. WebApi FormData+文件长传 异步+同步实现

    // POST api/values public async Task Post() { try { // 检查该请求是否含有multipart/form-data if (!Request.Con ...

  7. Protobuff java 文件生成命令

    protoc.exe -I./proto文件目录 --java_out=java文件目录 proto文件基于文件目录的全路径 protoc.exe -I./protoFolder --java_out ...

  8. java代码----I/O流从控制台输入信息判断并抛出异常

    package com.a.b; import java.io.*; public class Yu { public static void main(String[] args) throws I ...

  9. 局域网使用NAT进行测试第三方接口

      问题分析   在局域网内开发一些涉及到第三方的接口调用功能时(譬如:支付),需要对方服务器进行接口回调,接受对方发送过来的信息.问题来了,我们一般开发都是在内网,如何才能获取到外网返回的数据呢?如 ...

  10. MySQL重启端口被占用处理

    1,查看日志的ERROR 2018-05-23T01:26:59.230382Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set. 201 ...