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. [LeetCode&Python] Problem 563. Binary Tree Tilt

    Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...

  2. HPU第四次积分赛-L:A Winged Steed(完全背包)

    A Winged Steed 描述 有n种千里马,每一种都有若干匹,第i种马的颜值ai​,价格di​.现有m个牧马人要去挑选千里马,每一位牧马人对马的颜值都有要求:{所选马的颜值总和} ⩾Ai​.现在 ...

  3. (0)前端总结(HTML + CSS + JQ)

    HTML 1.<meta charset="UTF-8">  #设置页面编码,这个设置英文则现在国内浏览器会弹出是否要转换中文 2.<title>我的第一个 ...

  4. docker安装linux系统镜像

    推荐镜像 Centos/Debian/UbuntuCentOS:kinogmt/centos-ssh (默认用户名root,密码password,CentOS6.7)CentOS:tutum/cent ...

  5. xenserver开启虚拟机时提示找不到存储介质,强制关闭和重启都没用

    具体报错内容为: this vm needs storage that cannot be seen from that host 该错误的原因是该虚拟机使用了外部存储介质,例如,我就是因为当初规划x ...

  6. MySQL Partition--分区基础

    ================================================================================= Mysql在5.1版本时增加对分区表 ...

  7. jquery禁用form表单中的文本框

    //禁用form表单中所有的input[文本框.复选框.单选框],select[下拉选],多行文本框[textarea] function disableForm(formId, isDisabled ...

  8. 套接字选项——getsockopt和setsockopt

    这两个函数仅用于套接字 #include <sys/socket.h> int getsockopt(int sock, int level, int optname, void *opt ...

  9. PADS 脚本记录:关于 getObjects

    PADS 脚本记录:关于 getObjects GetObjects(plogObjectTypeComponent, "ZBOM*", False) 返回的一个对象,所有 ZBO ...

  10. jumpserver修改默认管理员账号名

    1.安装完毕jumpserver之后,默认管理员账号为admin 显然类似windows的administrator以及linux的root 把账号名改成别的 个人信息界面点击设置 修改为自己想要的用 ...