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 ...
随机推荐
- [NYIST737]石子合并(一)(区间dp)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=737 很经典的区间dp,发现没有写过题解.最近被hihocoder上几道比赛题难住了 ...
- leetcode:Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1即反转二叉树,代码如下: /** * Defin ...
- 【转】Android横竖屏切换问题
Android横竖屏切换总结(Android资料) Android横竖屏要解决的问题应该就两个: 一.布局问题 二.重新载入问题 1.布局问题:如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的 ...
- HDFS的基本shell操作,hadoop fs操作命令
(1)分布式文件系统 随着数据量越来越多,在一个操作系统管辖的范围存不下了,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,因此迫切需要一种系统来管理多台机器上的文件,这就是分布式文件管 ...
- 【Android】 PopupWindow使用小结
PopupWindow的很多用法网上比较多,我就不做过多解释了,只说下可能会遇到的问题,以及解决办法: 1.PopupWindow中的listview无响应 这个主要是因为show写在了set ...
- github概念和实战
fork: 通过fork操作,你将拥有了别人创建的repo的ownership,但是url却变成了/youraccount/repo,这时你将可以做git push操作 clone: 该命令是直接将r ...
- core--多线程
WINDOWS是一个多线程操作系统,所谓多线程,就是在同一时间里,有多个线程同时在运行.我们上一遍说到CPU的执行序列是严格按照顺序来执行,怎么能够同一时间来执行很多程序呢?在早期答案是:window ...
- 如何使用SQL Server链接服务器访问DB2 Server
首先,需要安装Microsoft OLE DB Provider for DB2 下载地址:http://download.microsoft.com/download/B/B/2/BB22098A- ...
- Bitset位图
位图(bitmap)就是用每一位来存放某种状态,适合于大规模数据但是数据状态又不是很多的情况下,通常来判断数据是否存在.位图的常见应用有两种: 1.存放大规模数据,例如腾讯的面试题,给40亿个unsi ...
- WEBUS2.0 In Action - 索引操作指南(2)
上一篇:WEBUS2.0 In Action - 索引操作指南(1) | 下一篇:WEBUS2.0 In Action - 搜索操作指南(1) 3. 添加.删除.撤销删除和修改文档 在WEBUS中要将 ...