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 ...
随机推荐
- 51 nod 1006 最长公共子序列Lcs
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1006 参考博客 :http://blog.csdn.net/yysdsy ...
- leetcode:Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- Toad
1. Toad 规矩: toad 不会违反, 限制, 扩大 你当前用户的权限, toad 不会影响你定义的关于instance的内容. 2. toad 可以执行大部分在 sql*plus 中执行的命令 ...
- 【转载】关于XML文档的xmlns、xmlns:xsi和xsi:schemaLocation
原文在: https://yq.aliyun.com/articles/40353 这里有转载:http://www.cnblogs.com/zhao1949/p/5652167.html 先来一段S ...
- POJ 2689
题意:求[l, r]区间中的间隔距离最大与最小的相邻两个素数,r<2200000000, r-l<10^6 题解: 对于<a的合数,其必然存在一个素因子b<=sqrt(a). ...
- SVN功能详解
SVN功能详解 TortoiseSVN是windows下其中一个非常优秀的SVN客户端工具.通过使用它,我们可以可视化的管理我们的版本库.不过由于它只是一个客户端,所以它不能对版本库进行权限管理. ...
- [转][TFS] 禁止默认允许多人签出和强制解除签入签出锁
转自:http://blog.xieyc.com/tfs-disable-multiple-check-out-and-force-to-undo-locking/ | 小谢的小站 [TFS] 禁止默 ...
- (转载)C语言预处理
C程序的源代码中可包括各种编译指令,这些指令称为预处理命令.虽然它们实际上不是C语言的一部分,但却扩展了C程序设计的环境.本节将介绍如何应用预处理程序和注释简化程序开发过程,并提高程序的可读性.ANS ...
- (C#基础) byte[] 之初始化, 赋值,转换。
byte[] 之初始化赋值 用for loop 赋值当然是最基本的方法,不过在C#里面还有其他的便捷方法. 1. 创建一个长度为10的byte数组,并且其中每个byte的值为0. byte[] myB ...
- 定时任务处理-Quartz
Quartz Scheduler,定时任务 Quartz是一个作业调度系统(a job scheduling system),负责在约定的时间到达时执行(或通知)其他软件控制.是一个Java的定时任务 ...