import java.util.HashMap;
import java.util.Map; public class LRUCache {
private int capacity;
private int len; class Data {
int key;
int value;
Data next;
Data pre;
} private Map<Integer, Data> dataSet; private Data head;
private Data rail; public LRUCache(int capacity) {
this.capacity = capacity;
this.len = 0;
this.head = null;
this.rail = null;
this.dataSet = new HashMap<Integer, Data>();
} public int get(int key) {
if (!dataSet.containsKey(key))
return -1;
Data temp = dataSet.get(key);
if (temp == head) {
return temp.value;
} else if (temp == rail) {
temp.pre.next = null;
rail = temp.pre;
} else {
temp.pre.next = temp.next;
temp.next.pre = temp.pre;
}
temp.next = head;
temp.pre = null;
head.pre = temp;
head = temp;
return temp.value; } public void set(int key, int value) {
if (capacity == 0)
return;
if (dataSet.containsKey(key)) {
Data temp = dataSet.get(key);
if (temp == head) {
temp.value = value;
dataSet.put(key, head);
return;
} else if (temp == rail) {
temp.pre.next = null;
rail = temp.pre;
} else {
temp.pre.next = temp.next;
temp.next.pre = temp.pre;
}
temp.value = value;
temp.next = head;
temp.pre = null;
head.pre = temp;
head = temp;
dataSet.put(key, head);
} else {
if (len == capacity) {
dataSet.remove(rail.key);
if (rail == head)
head = null;
else {
rail.pre.next = null;
rail = rail.pre;
}
len--;
} if (head == null) {
head = new Data();
head.key = key;
head.value = value;
head.next = null;
head.pre = null;
dataSet.put(key, head);
rail = head;
len++;
} else {
Data temp = new Data();
temp.key = key;
temp.value = value;
temp.next = head;
temp.pre = null;
head.pre = temp;
head = temp;
dataSet.put(key, head);
len++;
} } } public static void main(String args[]) {
LRUCache l = new LRUCache(2);
l.set(2, 1);
l.set(1, 1);
l.set(2, 3);
l.set(4, 1);
System.out.println(l.get(1));
System.out.println(l.get(2)); } }

【leetcode】LRU的更多相关文章

  1. 【LeetCode】LRU Cache 解决报告

    插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! Design and implement a data structure ...

  2. 【leetcode】LRU Cache

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

  3. 【leetcode】LRU Cache(hard)★

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

  4. 【Leetcode】 LRU Cache实现

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

  5. 【LeetCode】设计题 design(共38题)

    链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. C# Http以文件的形式上传文件

    以下的是上传的方法: // <summary> /// 将本地文件上传到指定的服务器(HttpWebRequest方法) /// </summary> /// <para ...

  2. HTTP 响应

    HTTP 响应 所谓响应事实上就是server对请求处理的结果.或者假设浏览器请求的直接就是一个静态资源的话,响应的就是这个资源本身. HTTP 响应的组成 ①响应状态行:包含协议版本号.响应状态码. ...

  3. Delphi实现窗口一直在桌面工作区内显示(重写WM_WINDOWPOSCHANGING消息)

    有的时候我们要实现一个悬浮窗口,并使该窗口一直显示在桌面的工作区内.即整个窗口要一直显示在屏幕上,不能超出屏幕的上下左右边缘.此功能的实现也不难,我们需要自己写代码来响应窗口的WM_WINDOWPOS ...

  4. Design Pattern Memo 备忘录设计模式

    本设计模式就是简单地记录当前状态.然后利用记录的数据恢复. 比方首先我们有一个类.类须要记录当前状态进行相关的工作的: class Memo; class Human { public: string ...

  5. POJ 3415 Max Sum of Max-K-sub-sequence (线段树+dp思想)

    Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  6. JavaScript引用类型之Object类

    ECMAScript中的Object类跟Java中的Object类相似,ECMAScript中的全部类都由这个类继承而来,Object类中的全部属性和方法都会出如今其他类中,所以理解Object类,就 ...

  7. ASA QOS限速

    cisco的Qos限速和H3C的有点区别,不过总体来说,H3C的比较渣,单位是不一样的,H3C 的CAR单位的是kpbs,而cisco police限速时的单位是Bits per seconds,H3 ...

  8. MBR格式无法识别2T以上的硬盘的问题

    早上有人打电话说四块2T的sata 硬盘做了raid 5之后安装window server 2012的时候,无法创建分区,安装完系统后无法给剩余的硬盘创建分区,刚开始的时候我还以为是别人分区的数量问题 ...

  9. Matlab实现PCA

    在主成分分析(PCA)中,介绍了PCA的数学原理,其有用Matlab能够非常方便地对矩阵进行操作! 比方,用Matlab求多个样本的协方差矩阵.求矩阵的特征根和特征向量等. 以下介绍用Matlab实现 ...

  10. Knockout应用开发指南 第五章:创建自定义绑定

    原文:Knockout应用开发指南 第五章:创建自定义绑定 创建自定义绑定 你可以创建自己的自定义绑定 – 没有必要非要使用内嵌的绑定(像click,value等).你可以你封装复杂的逻辑或行为,自定 ...