leetcode@ [146] LRU Cache (TreeMap)
https://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 (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.
class pair {
public int key;
public int value;
public pair(int k, int v) {
super();
this.key = k;
this.value = v;
}
public void setValue(int value) {
this.value = value;
}
}
class cmp implements Comparator {
public int compare(Object o1, Object o2) {
pair p1 = (pair) o1;
pair p2 = (pair) o2;
if(p1.key < p2.key) {
return -1;
} else if(p1.key == p2.key) {
if(p1.value == p2.value) {
return 0;
} else if(p1.value < p2.value) {
return -1;
} else {
return 1;
}
} else {
return 1;
}
}
}
public class LRUCache {
public Set<pair> stack = null;
public HashMap<Integer, Integer> mapping = null;
public TreeMap<Integer, Integer> timeToKey = null;
public TreeMap<Integer, Integer> keyToTime = null;
public int cap = 0;
public int counter = 0;
public LRUCache(int capacity) {
this.mapping = new HashMap<Integer, Integer> ();
this.timeToKey = new TreeMap<Integer, Integer> ();
this.keyToTime = new TreeMap<Integer, Integer> ();
this.cap = capacity;
this.counter = 0;
}
public int get(int key) {
if(!mapping.containsKey(key)) {
return -1;
} else {
counter++;
int value = mapping.get(key);
int time = keyToTime.get(key);
keyToTime.put(key, counter);
timeToKey.remove(time);
timeToKey.put(counter, key);
return value;
}
}
public void set(int key, int value) {
counter++;
if(mapping.containsKey(key)) {
int time = keyToTime.get(key);
keyToTime.put(key, counter);
timeToKey.remove(time);
timeToKey.put(counter, key);
mapping.put(key, value);
} else {
if(mapping.size() < cap) {
mapping.put(key, value);
keyToTime.put(key, counter);
timeToKey.put(counter, key);
} else {
int lru = timeToKey.pollFirstEntry().getValue();
mapping.remove(lru);
mapping.put(key, value);
keyToTime.put(key, counter);
timeToKey.put(counter, key);
}
}
}
}
leetcode@ [146] LRU Cache (TreeMap)的更多相关文章
- leetcode 146. LRU Cache 、460. LFU Cache
LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...
- [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- Java for LeetCode 146 LRU Cache 【HARD】
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- [LeetCode] 146. LRU Cache 近期最少使用缓存
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- leetcode 146. LRU Cache ----- java
esign and implement a data structure for Least Recently Used (LRU) cache. It should support the foll ...
- Leetcode#146 LRU Cache
原题地址 以前Leetcode的测试数据比较弱,单纯用链表做也能过,现在就不行了,大数据会超时.通常大家都是用map+双向链表做的. 我曾经尝试用C++的list容器来写,后来发现map没法保存lis ...
- 【LeetCode】146. LRU Cache 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+双向链表 日期 题目地址:https://le ...
- 【LeetCode】146. LRU Cache
LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should suppo ...
- LeetCode之LRU Cache 最近最少使用算法 缓存设计
设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...
随机推荐
- TFSAPI
Team Foundation Server (TFS)工具的亮点之一是管理日常工作项, 工作项如Bug, Task,Task Case等. 使用TFS API编程访问TFS服务器中的工作项, 步骤如 ...
- php注册登录系统(一)-极简
序 登录注册系统是日常上网最普通的操作,我设了一个分类一步步完善注册登录系统,若有哪里错误请慧教 所用语言:php 数据库 :mysql 本次实现功能: 1.用户注册 2.用户登录 主要文件: 完整代 ...
- 【转】Android 使用ORMLite 操作数据库
Android 使用ORMLite 操作数据库 用过ssh,s2sh的肯定不会陌生 ,应该一学就会 第一步: 下载ormlite-android-4.41.jar和ormlite-core-4.4 ...
- Oracle数据库之三
子查询 -- 就是在一个查询中包含多个select语句(一般就2个) select id,first_name,dept_id from s_emp; 想查询和Ben一个部门的员工的id,first_ ...
- number-of-boomerangs
https://leetcode.com/problems/number-of-boomerangs/ package com.company; import java.util.*; class S ...
- 红黑树、B(+)树、跳表、AVL等数据结构,应用场景及分析,以及一些英文缩写
在网上学习了一些材料. 这一篇:https://www.zhihu.com/question/30527705 AVL树:最早的平衡二叉树之一.应用相对其他数据结构比较少.windows对进程地址空间 ...
- how bootstrap fit into our website design?
在web相关技术中,HTML代表了content,structure, CSS则负责styling,决定内容是如何展示的,javascript则主要负责interactive, behaviour. ...
- 51nod1225 余数之和
打表可以看出规律.分块求就可以了. #include<cstdio> #include<cstring> #include<cctype> #include< ...
- NBUT 1122 Shameimaru's Candid Camera(水)
题意: 给n*m个格子,初始时每个格子中有个数值为0,部分格子中含有炸弹,每个炸弹爆炸可以将周围的8个非炸弹格子中的数值加1,求全部炸弹炸完后那些非0且非炸弹格子中的数是多少. 思路: 另开一个矩阵, ...
- 正则化,数据集扩增,Dropout
正则化方法:防止过拟合,提高泛化能力 在训练数据不够多时,或者overtraining时,常常会导致overfitting(过拟合).其直观的表现如下图所示,随着训练过程的进行,模型复杂度增加,在tr ...