5月18日:top10面试算法-LRUcache的实现
问题描述:
LRU算法:优先把那些最长时间没使用的对象置换掉。根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高”。
JAVA实现:
测试:
public class preface {
public static void main(String[] args) {
LRUCache cache = new LRUCache();
cache.set(1, "one");
cache.set(2, "two");
cache.set(3, "three");
cache.set(5, "five");
cache.set(4, "five");
cache.get(2);
cache.set(6, "six");
cache.printCache();// 2,3,4,5,6
}
}
仓促之下写了个脑残版的,到头来也没能正确运行:
public class LRUCache {
private Map<Integer, pair> cache;
private int _cur; //计数
private int _capacity;
private int _nums = 0;
private heap _h;
public class pair implements Comparable {
public String value;
public int num;
public pair(int num, String value) {
this.num = num;
this.value = value;
}
@Override
public int compareTo(Object o) {
return num - ((pair) o).num;
}
}
//通过堆来对pairs进行排序
public class heap {
public pair[] pairs;
private int _cur;
public heap() {
pairs = new pair[5];
for (int i = 0; i < 5; ++i) {
pairs[i] = new pair(0, null);
}
_cur = 0;
}
//每次只能修改最上面的pair
public pair set(pair p) {
pair t = pairs[0];
pairs[0] = p;
return t;
}
public void insert(pair p) {
if (_cur < 5) {
pairs[_cur] = p;
}
++_cur;
}
public void sort() {
Arrays.sort(pairs);
}
public int find(pair p) {
for (int i = 0; i < 5; ++i) {
if (pairs[i] == p) { //==比较的是地址,equal比较的是值
return i;
}
}
return -1;
}
}
public LRUCache() {
_cur = 0;
_capacity = 0;
cache = new HashMap<Integer, pair>();
_h = new heap();
}
public String get(int key) {
++_cur;
if (cache.containsKey(key)) {
return cache.get(key).value;
}
return null;
}
public void set(int key, String value) {
++_cur;
if (!cache.containsKey(key)) {
++_nums;
pair p = new pair(_cur, value);
if (_nums == _capacity) {
pair t = _h.set(p);
for (Integer k:cache.keySet()) {
if (cache.get(k) == t) {
cache.remove(t);
break;
}
}
} else {
_h.insert(p);
}
cache.put(key, p);
} else {
pair p = cache.get(key);
int t = _h.find(p);
_h.pairs[t].num = _cur;
_h.pairs[t].value = value;
}
_h.sort();
}
public void printCache() {
System.out.println("print cache usage");
for (Integer key:cache.keySet()) {
pair p = cache.get(key);
System.out.println("Key:" + key + " Value:" + p.value + " num:" + p.num);
}
}
}
C++实现:
struct DoubleLinkList {
int key;
int value;
DoubleLinkList *next;
DoubleLinkList *pre;
DoubleLinkList(int k, int v) : key(k), value(v), next(NULL), pre(NULL) {};
};
class LRUCache{
public:
LRUCache(int capacity) {
size = capacity;
cacheMap.clear();
head = new DoubleLinkList(0, 0);
tail = new DoubleLinkList(0, 0);
head->next = tail;
tail->pre = head;
}
int get(int key) {
if (cacheMap.find(key) != cacheMap.end()) {
DoubleLinkList *p = cacheMap[key];
spliceNode(p);
addToFront(p);
return p->value;
}
return -1;
}
void set(int key, int value) {
if (cacheMap.find(key) == cacheMap.end()) {
DoubleLinkList *p = NULL;
if (cacheMap.size() == size) {
p = tail->pre;
cacheMap.erase(p->key);
p->key = key;
p->value = value;
spliceNode(p);
addToFront(p);
} else {
p = new DoubleLinkList(key, value);
addToFront(p);
}
cacheMap[key] = p;
} else {
DoubleLinkList *p = cacheMap[key];
p->value = value;
spliceNode(p);
addToFront(p);
}
}
private:
int size;
map<int, DoubleLinkList*> cacheMap;
DoubleLinkList *head, *tail;
void addToFront(DoubleLinkList *p) {
p->pre = head;
p->next = head->next;
head->next->pre = p;
head->next = p;
}
void spliceNode(DoubleLinkList *p) {
p->pre->next = p->next;
p->next->pre = p->pre;
}
};
用list代替双向链表:
struct CacheNode {
int key;
int value;
CacheNode(int k, int v) : key(k), value(v) {
}
};
class LRUCache{
public:
LRUCache(int capacity) {
size = capacity;
}
int get(int key) {
if (cacheMap.find(key) != cacheMap.end()) {
auto it = cacheMap[key];
cacheList.splice(cacheList.begin(), cacheList, it);
cacheMap[key] = cacheList.begin();
return cacheList.begin()->value;
} else {
return -1;
}
}
void set(int key, int value) {
if (cacheMap.find(key) == cacheMap.end()) {
if (cacheList.size() == size) {
cacheMap.erase(cacheList.back().key);
cacheList.pop_back();
}
cacheList.push_front(CacheNode(key, value));
cacheMap[key] = cacheList.begin();
} else {
auto it = cacheMap[key];
it->value = value;
cacheList.splice(cacheList.begin(), cacheList, it);
cacheMap[key] = cacheList.begin();
}
}
private:
int size;
list<CacheNode> cacheList;
unordered_map<int, list<CacheNode>::iterator > cacheMap;
};
5月18日:top10面试算法-LRUcache的实现的更多相关文章
- 深度学习DeepLearning技术实战(12月18日---21日)
12月线上课程报名中 深度学习DeepLearning(Python)实战培训班 时间地点: 2020 年 12 月 18 日-2020 年 12 月 21日 (第一天报到 授课三天:提前环境部署 电 ...
- 2016年12月18日 星期日 --出埃及记 Exodus 21:13
2016年12月18日 星期日 --出埃及记 Exodus 21:13 However, if he does not do it intentionally, but God lets it hap ...
- 2015年8月18日,杨学明老师《技术部门的绩效管理提升(研讨会)》在中国科学院下属机构CNNIC成功举办!
2015年8月18日,杨学明老师为中国网络新闻办公室直属央企中国互联网络中心(CNNIC)提供了一天的<技术部门的绩效管理提升(研讨会)>培训课程.杨学明老师分别从研发绩效管理概述.研发绩 ...
- 2016年11月18日 星期五 --出埃及记 Exodus 20:9
2016年11月18日 星期五 --出埃及记 Exodus 20:9 Six days you shall labor and do all your work,六日要劳碌作你一切的工,
- 2016年10月18日 星期二 --出埃及记 Exodus 19:2
2016年10月18日 星期二 --出埃及记 Exodus 19:2 After they set out from Rephidim, they entered the Desert of Sina ...
- 天津Uber优步司机奖励政策(1月18日~1月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 长沙Uber优步司机奖励政策(1月18日~1月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 西安Uber优步司机奖励政策(1月18日~1月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 上海Uber优步司机奖励政策(1月18日~1月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
随机推荐
- Spring表达式语言 之 5.1 概述 5.2 SpEL基础(拾叁)
5.1 概述 5.1.1 概述 Spring表达式语言全称为"Spring Expression Language",缩写为"SpEL",类似于Struts ...
- js二级导航
js写二级导航要点 1.ul li 2.js获取元素 3.setInterval(function(),time); 代码如下 <style type="text/css"& ...
- [saiku] 通过 saiku 的 DEMO 分析 connection
示例:FOODMART connection: foodmart catalog: FoodMart schema: FoodMart cube: Sales/HR/Sales 2/.../ ==== ...
- css3做的动画
基于放大缩小: http://www.credero.etall.cn/demo/jwtplay/3/content05.html 基于3d: http://www.credero.etall.cn/ ...
- bug:C#线程间操作无效: 从不是创建控件" XX" 的线程访问它
今天遇到这个问题,百度了下,把解决的方法总结出来.我们在ui线程创建的子线程操作ui控件时,系统提示错误详细信息为:线程间操作无效: 从不是创建控件“XXX”的线程访问它. 就我知道的有三种方法,先看 ...
- Merge k Sorted Lists [LeetCode]
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. S ...
- content.boundingRectWithSize计算出来的高度不准
计算出来的高度会少一行的高度,最后一行会显示不全.减掉padding会解决这个问题. let padding = self.reviewText.textContainer.lineFragm ...
- Touch ID集成
作者感言 这个国庆由于种种原因, 过的不太安稳, 搬家, 办证, 东跑西跑, 忙的压根就不像是在过节....不过算了, 挑最后一天写写博文.最后:如果你有更好的建议或者对这篇文章有不满的地方, 请联系 ...
- iOS应用架构现状分析
iOS从2007年诞生至今已有近10年的历史,10年的时间对iOS技术圈来说足够产生相当可观的沉淀,尤其这几年的技术分享氛围无论国内国外都显得异常活跃.本文就iOS架构这一主题,结合开发圈里讨论较多的 ...
- ios基础篇(九)——自定义UITabBar
上一篇讲到了UITabBarViewController,接着说说UITabBarViewController中怎么自定义TabBar. 今天仿写了微博,发现底部tabbar中间的button和其他有 ...