LRU Cache
2015-11-23 19:58
239 查看
题目: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.
思路:
本题的一大特色是使用了双向链表。并且含有两个函数,spilt 和 insert 函数,插入就是把一个节点插入头结点之后的操作函数,而分开就是把当前节点从原有链表中分离出来。需要说明的是:head 和 tail 节点并不算任何节点。
首先是get 函数:
首先用哈希表查找是否存在,不存在直接返回-1。如果存在,则由于访问了当前数据,先是分离当前节点,再将其插入到最前面。最后返回其值。其次是set 函数:
先是使用get 函数判断是否存在,注意:如果存在,get 函数已经进行分离和插入操作了。如果不存在,还需要判断是否超过大小。这里使用hash表的大小判断。没有越界,直接赋值新节点,越界了,先是分离,再插入新节点。
代码:
class LRUCache{private:
struct Node{
int key;
int value;
Node *prev,*next;
Node(int k,int v):key(k),value(v),prev(NULL),next(NULL){}
};
unordered_map<int , Node*>mapper;
Node *head,*tail;
int capacity;
void insert(Node *target){
target->next=head->next;
head->next->prev=target;
head->next=target;
target->prev=head;
}
void split(Node *target){
target->prev->next=target->next;
target->next->prev=target->prev;
//insert(target);
}
public:
LRUCache(int capacity) {
this->capacity=capacity;
head=new Node(0,0);
tail=new Node(0,0);
head->next=tail;//设立开头结尾指针,方便操作
tail->prev=head;
}
int get(int key) {
if(mapper.find(key)==mapper.end()){
return -1;
}
Node *target=mapper[key];
if(target!=head->next){//如果不是第一个,那么根据最小使用原则,使用了一次target,把他挪到
//最前面
split(target);
insert(target);
}
return target->value;
}
void set(int key, int value) {
if(get(key)!=-1){// 数值已经存在
head->next->value=value;//get 这个函数里面已经有分离 插入 这些操作了.
return;
}
//下面判断不存在,但是要考虑是否越界
Node *tmp=NULL;
if(mapper.size()==capacity){
tmp=tail->prev;
split(tmp);
mapper.erase(tmp->key);
tmp->value=value;
tmp->key=key;
}/*
tmp->value=value;
tmp->key=key;*/ // 不能这么写,因为上面定义的tmp是一个单链表指针,这样赋值是双链表指针
tmp=new Node(key,value);
mapper[key]=tmp;
insert(tmp);
}
};
相关文章推荐
- php之输出缓存ob
- error:LNK2005 已经在*.obj中定义
- 势头迅猛的儿童手表:恐陷下一个文曲星之地?
- 指针的学习
- appStore检查版本更新与从app里跳到appstore评论页面
- iar 错误解决
- codeforces 581C Developing Skills
- Java问题排查常用linux命令
- 数据结构之二叉树_BinTree
- 为了能过个好年,xhd开始复习了,于是每天晚上背着书往教室跑。xhd复习有个习惯,在复习完一门课后,他总是挑一门更简单的课进行复习,而他复习这门课的效率为两门课的难度差的平方,而复习第一门课的效率为1
- Codeforces 299 B Spongebob and Joke
- c#多线程__Semaphore
- 20135324家庭作业汇总(2.68、2.70与20135330合作,4.58、6.32与20135332合作)
- 如何修改mtk android 默认拍照size
- Android——广播
- Bootstrap 基本按钮
- 在linux系统下搭建SVN服务器
- 3d打印(5):OpenSCAD软件学习
- 学习笔记
- leetcod_194_transpose_file