题意

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.

这道题的题意就是说,使用LRU Cache,就是将最新使用的放置在前面,以便下次取的时候方便,其余则依次往下移动;

思路

其实我也是根据别人的想法做的,用链表加入值,因为链表对于往前往后添加或者删除都非常方便,但是有个麻烦的地方在于,我并不知知道他的位置在哪里,只能循环去找,因此我使用了一个哈希数组进行存储她的key和下标;

实现

我的实现(比较白痴)

struct Node {
int key;
int value;
Node(int _k, int _v) {key = _k; value = _v;}
}; class LRUCache{
public:
unordered_map<int, int> maps;
list<Node> lists;
int size;
LRUCache(int capacity) {
size = capacity;
} /**
* 获取值,同时获取也算是一种访问
*
* @param key <#key description#>
*
* @return <#return value description#>
*/
int get(int key) {
if (maps.size() == 0) {
return -1;
}
if (maps.find(key) != maps.end()) {
auto key_value = maps.find(key);
int j = 0;
for (auto itr = lists.begin(); itr != lists.end() && j < lists.size(); itr++, j++) {
if (j == key_value->second) {
int value = (*itr).value;
lists.push_front(Node((*itr).key, (*itr).value));
// 更新lists和maps
lists.erase(itr); // 将要更新的迭代器之前的下标都要加上1,其余的位置不需要改变
for (auto mitr = maps.begin(); mitr != maps.end(); mitr++) {
if (mitr->second < key_value->second){
mitr->second++;
}
} // 更新全部位置
key_value->second = 0; return value;
}
}
}
return -1;
} /**
* 添加新值,不过需要注意的是当内存不够的情况下,需要删除掉最不经常使用的
*
* @param key <#key description#>
* @param value <#value description#>
*/
void set(int key, int value) {
if (lists.size() == size) {
// 满了先进行删除
list<Node>::iterator lend = --lists.end();
lists.pop_back(); int lkey = lend->key;
maps.erase(maps.find(lkey)); lists.push_front(Node(key, value)); for (auto mitr = maps.begin(); mitr != maps.end(); mitr++) {
mitr->second++;
} maps.insert(make_pair(key, 0));
}
else {
// 存在的话
if (maps.find(key) != maps.end()) {
auto key_value = maps.find(key);
maps[key] = 0;
for (auto mitr = maps.begin(); mitr != maps.end(); mitr++) {
if (mitr->second < key_value->second){
mitr->second++;
}
}
// 更新全部位置
key_value->second = 0;
}
else {
lists.push_front(Node(key, value));
for (auto mitr = maps.begin(); mitr != maps.end(); mitr++) {
mitr->second++;
}
maps.insert(make_pair(key, 0));
}
}
}
}; int main(int argc, const char * argv[]) {
// insert code here...
LRUCache cache(5);
cache.set(1, 10);
cache.set(2, 20);
cache.set(3, 30); //cout << "cache..." << cache.get(2) << endl; cache.set(4, 40);
cache.set(5, 50); cache.set(8, 80); //cout << "cache..." << cache.get(1) << endl;
cout << "cache..." << cache.get(5) << endl; return 0;
}

如上面所说,我需要不停的去更新其的位置,这样才能根据正确的位置去获取到具体的结点信息;根据题目的要求,需要控制时间复杂度在1,所以需要需要一个哈希数组去记录其位置;但是我好像并没有做到,依旧使用了一个循环。。。

我的基础上优化

struct node{
int key;
int value;
node(int k, int v):key(k), value(v){}
}; /*
* 注意整体思路是,使用双向list每次set或get一个元素时都把这个元素放到list的头部,无需统计每个元素的操作次数,实际上LRU的意思
* 就是根据元素最后被访问的时间来决定替换哪个,故list中尾部元素即被替换.
* STL技巧:1、使用map的find方法来判断key是否已经存在,返回值和map的end迭代器比较;
2、使用unordered_map,它是hash_map,存取时间都是O(1),用它存储元素的position迭代器,是为了方便splice函数调用
* list.splice(position, list, element_pos)函数作用是把list的element_pos处的元素插入到position位置,本题中
为了移动元素到list头部
*/
class LRUCache{
int size;
list<node> values;
unordered_map<int, list<node>::iterator> positions;
public:
LRUCache(int capacity) {
size = capacity;
} int get(int key) {
if(positions.find(key) != positions.end()){
values.splice(values.begin(), values, positions[key]);
positions[key] = values.begin(); return values.begin()->value;
}
return -1;
} void set(int key, int value) {
if(positions.find(key) != positions.end()){
values.splice(values.begin(), values, positions[key]); //移动被访问元素到头部
values.begin()->value = value;
positions[key] = values.begin(); //更新其位置,注意此处的position只是一个指针,当此key在list中被挤到其他位置后,positions里保存的位置也会跟着变化,因为它仅仅是一个指向该结点的指针
}
else if(values.size()<size){
values.push_front(node(key, value));
positions[key] = values.begin();
}
else{
node last = values.back();
values.pop_back();
positions.erase(last.key); values.push_front(node(key, value));
positions[key] = values.begin();
}
} };

将我的int下标,改成了纪录链表的迭代器,这下使用了splice会很方便,同时控制了时间复杂度。

总结

并没有。

LRU Cache 题解的更多相关文章

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

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

  2. 146. LRU Cache

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

  3. LeetCode解题报告:LRU Cache

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

  4. LRU Cache leetcode java

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

  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 the fol ...

  7. 【leetcode】LRU Cache

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

  8. LeetCode:LRU Cache

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

  9. LRU Cache实现

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

随机推荐

  1. 2015年5月9日 student information management system

    /*大作业SIMS*///头文件 #ifndef __FUNC_C__ #define __FUNC_C__ #include <stdio.h> #include <stdlib. ...

  2. thinkPHP16---伪静态

    url伪静态通常是为了 满足更好的SEO效果,thinkPHP支持伪静态url设置,可以通过设置URL_HTML_SUFFIX的参数 随意在URL的最后添加你想要的静态后缀,而不会影响当前操作的正常执 ...

  3. 转:Loadrunner学习知多少--脚本录制下载操作

    在很多时候我们可能需要对系统进行这样的脚本开发,模拟用户点击一个下载链接,然后弹出下载框,选择保存,用来测试在大量用户下载时服务器的性能.但是现在大家对于这种脚本的处理方式往往是通过关联和C 语言的文 ...

  4. Listview源码分析(1)

    首先Listview继承关系: ListView  --extends-->  AbsListview  --extends-->  AdapterView  --extends--> ...

  5. 优化之sitemap+RSS

    RSS也叫聚合, RSS是在线共享内容的一种简易方式,也叫聚合内容,Really Simple Syndication. 通常在时效性比较强的网站或网络平台上应用RSS订阅功能可以更快速获取信息,网站 ...

  6. Linux中java项目环境部署,简单记录一下

    这里只是简单的记录一下linux环境下面如何快速的搭配好环境,使你的项目能在linux环境上面运行. 很多时候,我们都是用windows环境进行配置调试的,而真正很多服务器都是在linux服务器上面的 ...

  7. VNC轻松连接远程Linux桌面

    VNC连接Linux桌面,要想连接Linux远程桌面,按照下面的步骤,非常简单.快速,Linux配置VNC(以RedHat.CentOS.Fedora系列为例). 工具/原料 Linux平台安装VNC ...

  8. [code]字母重排

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  9. postfix+dovecot配置多域名邮件服务器

    mail邮局系统的MX(邮件交换)记录配置,以便收发邮件.(MX记录,是邮件交换记录,它指向一个邮件服务器,用于电子邮件系统发邮件时根据收信人的地址后缀来定位邮件服务器,如果没有做域名解析,邮局不能正 ...

  10. CF 672 div2 D

    http://codeforces.com/contest/672/problem/D 题目大意: 有n个人,每个人有pi的钱,然后可以由如下操作,每次都可以挑选一个最富有的人,把它的钱给最穷的人.但 ...