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. Swift - 访问控制(private,internal,public)

    在Swift语言中,访问修饰符有三种,分别为private,internal和public.同时,Swift对于访问权限的控制,不是基于类的,而是基于文件的.其区别如下: 1,private priv ...

  2. Swift - 属性观察者(willSet与didSet)

    属性观察者,类似于触发器.用来监视属性的除初始化之外的属性值变化,当属性值发生改变时可以对此作出响应.有如下特点: 1,不仅可以在属性值改变后触发didSet,也可以在属性值改变前触发willSet. ...

  3. VS2012配置astyle格式化代码

    1.工具->扩展和更新,搜astyle插件,下载安装重启,当前是2.0版本. 2.工具->选项->AStyle Formatter->Edit,填入下面的,点击save,确定. ...

  4. QT 多线程程序设计(也有不少例子)

    QT通过三种形式提供了对线程的支持.它们分别是,一.平台无关的线程类,二.线程安全的事件投递,三.跨线程的信号-槽连接.这使得开发轻巧的多线程Qt程序更为容易,并能充分利用多处理器机器的优势.多线程编 ...

  5. Setup SSH and SVN on Windows Server

    cygwin: install sshd, cygrunsrv http://lifehacker.com/205090/geek-to-live--set-up-a-personal-home-ss ...

  6. VC++ 视频播放器 图文步骤记录

    1.安装DirectShow9.0 SDK DirectShow9 SDK下载链接http://download.csdn.net/detail/jindou910101/5591169 2.运行Di ...

  7. HDU1385 【输出字典序最小的最短路】

    这题经过的结点比较好处理. 主要是字典序的处理. 先是floyd做法,采用记录后驱的方法.  path[i][j]=j[初始化...] #include <iostream> #inclu ...

  8. Samba &amp; Nginx - Resource temporarily unavailable

    先说说本人的开发环境:Win7 + Editplus + VMware(Centos+Samba+Nginx).用Samba在Centos上把web文件夹(如www)共享,然后在Win7上訪问这个文件 ...

  9. nginx fastcgi 自定义错误页面

    http{ fastcgi_intercept_errors on; error_page 404 /404.html; } fastcgi_intercept_errors on;必须设置 之后通过 ...

  10. 诺贝尔物理学奖公布:LED灯将点亮了整个21世纪

    很多其它精彩.破晓博客:点击打开链接 7日.在瑞典首都斯德哥尔摩,瑞典皇家科学院常任秘书诺尔马克(左二)宣布2014年诺贝尔物理学奖得主.新华社发 ■人物 中村修二 勇于追讨酬劳的科学家 被誉为&qu ...