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. 如何利用Win32API取得另一支程式中的ListView內的所有值(RegisterHotKey,ReadProcessMemory,WindowFromPoint和VirtualAllocEx)

    http://blog.csdn.net/shuaihj/article/details/6129506

  2. VC调试篇

    难怪很多前辈说调试是一个程序员最基本的技能,其重要性甚至超过学习一门语言.不会调试的程序员就意味着他即使会一门语言,却不能编制出任何好的软件. 我以前接触的程序大多是有比较成形的思路和方法,调试起来出 ...

  3. Static关键字的作用及使用

    1.使用static声明属性 如果希望一个属性被所有对象共同拥有,可以将其声明为static类型. 声明为static类型的属性或方法,此属性或方法也被称为类方法,可以由类名直接调用. class P ...

  4. SpringMVC+Spring3+Hibernate4开发环境的搭建

    在项目早期比较简单,大多用JSP .Servlet + JDBC 直接获取,以后使用 Struts1(Struts2)+Spring+Hibernate, 严格格按照分层概念驱动项目开发.利用这段时间 ...

  5. Java抓取网页数据(原来的页面+Javascript返回数据)

    转载请注明出处! 原文链接:http://blog.csdn.net/zgyulongfei/article/details/7909006 有时候因为种种原因,我们须要採集某个站点的数据,但因为不同 ...

  6. Leetcode-Database-176-Second Highest Salary-Easy(转)

    leetcode地址:https://oj.leetcode.com/problems/second-highest-salary/ 这个问题很有趣,是要求我们写个sql来查询Employee表里第二 ...

  7. sprintf,多少钱你知道?

    选<CSDN 社区电子杂志——C/C++杂志>http://emag.csdn.net 2005 年1 月 总号1 期 - 93 -笔者:steedhorse(晨星)printf 可能是很 ...

  8. 开玩笑Web它servlet(五岁以下儿童)---- 如何解决servlet线程安全问题

    servlet默认值是安全线的存在,但说白,servlet安全线实际上是一个多线程线程安全问题.因为servlet它正好是一个多线程的安全问题出现. 每次通过浏览器http同意提交请求,将一个实例se ...

  9. 浏览器url传参中文时得到null的解决方法

    在写一个中文参数需求的时候遇到了以下问题,经过半天的测试和各种编码,以及网上一些有用没用的资料尝试终于解决    比如下面的url地址:http://travel.widget.baike.com:8 ...

  10. HTML学习笔记——各种居中对齐

    0.前言     水平居中基本方法--指定块的宽度并设定块的左右外边距为auto,上下外边距可取0,那么该块能够在父元素中水平居中. 样式例如以下: 1:margin:0px auto 2:margi ...