Problem

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.

Java Solution

The key to solve this problem is using a double linked list which enables us to quickly move nodes.

import java.util.HashMap;

public class LRUCache {
private HashMap<Integer, DoubleLinkedListNode> map
= new HashMap<Integer, DoubleLinkedListNode>();
private DoubleLinkedListNode head;
private DoubleLinkedListNode end;
private int capacity;
private int len; public LRUCache(int capacity) {
this.capacity = capacity;
len = 0;
} public int get(int key) {
if (map.containsKey(key)) {
DoubleLinkedListNode latest = map.get(key);
removeNode(latest);
setHead(latest);
return latest.val;
} else {
return -1;
}
} public void removeNode(DoubleLinkedListNode node) {
DoubleLinkedListNode cur = node;
DoubleLinkedListNode pre = cur.pre;
DoubleLinkedListNode post = cur.next; if (pre != null) {
pre.next = post;
} else {
head = post;
} if (post != null) {
post.pre = pre;
} else {
end = pre;
}
} public void setHead(DoubleLinkedListNode node) {
node.next = head;
node.pre = null;
if (head != null) {
head.pre = node;
} head = node;
if (end == null) {
end = node;
}
} public void set(int key, int value) {
if (map.containsKey(key)) {
DoubleLinkedListNode oldNode = map.get(key);
oldNode.val = value;
removeNode(oldNode);
setHead(oldNode);
} else {
DoubleLinkedListNode newNode =
new DoubleLinkedListNode(key, value);
if (len < capacity) {
setHead(newNode);
map.put(key, newNode);
len++;
} else {
map.remove(end.key);
end = end.pre;
if (end != null) {
end.next = null;
} setHead(newNode);
map.put(key, newNode);
}
}
}
} class DoubleLinkedListNode {
public int val;
public int key;
public DoubleLinkedListNode pre;
public DoubleLinkedListNode next; public DoubleLinkedListNode(int key, int value) {
val = value;
this.key = key;
}
}

  

ps:

存在并发问题。

LeetCode – LRU Cache (Java)的更多相关文章

  1. leetcode 146. LRU Cache ----- java

    esign and implement a data structure for Least Recently Used (LRU) cache. It should support the foll ...

  2. [LeetCode] LRU Cache 最近最少使用页面置换缓存器

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

  3. LeetCode——LRU Cache

    Description: Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...

  4. [LeetCode]LRU Cache有个问题,求大神解答【已解决】

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

  5. Leetcode: LRU Cache 解题报告

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

  6. LeetCode:LRU Cache

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

  7. LeetCode: LRU Cache [146]

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

  8. [LeetCode] LRU Cache [Forward]

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

  9. LRU Cache java实现

    要求: get(key):如果key在cache中,则返回对应的value值,否则返回null set(key,value):如果key不在cache中,则将该(key,value)插入cache中( ...

随机推荐

  1. 使用httputil中ReverseProxy反向代理遇到的坑

    坑描述,当POST ContentType=="application/x-www-form-urlencoded"时,反向代理报错:http: proxy error: http ...

  2. 改变html中的内容

    $("#id").html()            获取内容 $("#id").html(xiugai)    修改内容

  3. struts2简单类型参数转换器(拦截器自动转换)

    这边测试类型int,string,date,list(set),map,下面贴代码 struts.xml文件代码 <!-- 类型转换 --> <action name="C ...

  4. Go Example--if语句

    package main import "fmt" func main() { //if else 条件都不需要括号,{}是需要的 if 7 % 2 == 0 { fmt.Prin ...

  5. centos7 部署elasticsearch

    环境: 系统:centos7.3 版本:elasticsearch6.2.3 head版本:https://codeload.github.com/mobz/elasticsearch-head/zi ...

  6. pnpm 快速节省磁盘工具的包管理工具

    nodejs 相关的包管理工具有很多,我们常用的有 npm cnpm(我基本已经不用了),yarn... pnpm 是另外一个不错的包管理工具,包含以下特性 快速 节省空间,一个版本的包只会在磁盘中存 ...

  7. loki grafana 团队开源的,类似Prometheus 的log 系统

    Prometheus 主要面向的是metrics,但是loki 是log,这样加上grafana 强大的可视化以及alert能力, 我们可以做好多事情,loki 的设计来源于Prometheus. 组 ...

  8. IAR intrinsic functions

    You can insert asm code example asm("NOP") into the c or c++ source code to get a good per ...

  9. lch 儿童围棋课堂 初级篇2 (李昌镐 著)

    第1章 吃子 第2章 死活:实战演练 第3章 劫争 第4章 布局:定式篇 第5章 布局:三线,四线和大场 第6章 布局:布局类型与形势判断 第7章 中盘:分断 第8章 官子:价值的计算 第9章 对杀技 ...

  10. c# 数据类型可在在最后的带一个字母

    folat的后面要带F或者f: double的后面要带D或者d: decimal的后面要带M或者m: long的后面要带L或者l: