【leetcode】LRU
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的更多相关文章
- 【LeetCode】LRU Cache 解决报告
插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! Design and implement a data structure ...
- 【leetcode】LRU Cache
题目简述: Design and implement a data structure for Least Recently Used (LRU) cache. It should support t ...
- 【leetcode】LRU Cache(hard)★
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 【Leetcode】 LRU Cache实现
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 【LeetCode】设计题 design(共38题)
链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- Swift - 协议(protocol)
1,Swift中协议类似于别的语言里的接口,协议里只做方法的声明,包括方法名.返回值.参数等信息,而没有具体的方法实现. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- Swift - 区间运算符(... 和 ..<)
区间运算符可以用来表示两个操作数之间的范围集合. 1,闭区间运算符:a...b 1 2 3 for iCount in 512...1024{ //从512遍历到1024(包括1024) } ...
- Suse 创建NFS共享目录
Suse 创建NFS共享目录 服务端的配置: 1.编辑nfs服务的配置文件 /software/suse11 *(rw,sync,no_root_squash,no_all_squash) 凝视: / ...
- Android Studio中如何打JAR包
Android Studio中对于library类型的Moudle,默认打出来的是AAR包, 但有时候我们的SDK还需要共享给一些其他eclipse的项目使用,这样我们就需要输出JAR包, 可以通过在 ...
- Mysql iot表
我们知道一般的表都以堆(heap)的形式来组织的,这是无序的组织方式. Oracle还提供了一种有序的表,它就是索引组织表,简称IOT表.IOT表上必须要有主键,而IOT表本身不对应segment,表 ...
- linux内存基础知识和相关调优方案
内存是计算机中重要的部件之中的一个.它是与CPU进行沟通的桥梁. 计算机中全部程序的执行都是在内存中进行的.因此内存的性能对计算机的影响很大.内存作用是用于临时存放CPU中的运算数据,以及与硬盘等外部 ...
- ruby中的模块
什么是模块 模块(module)是Ruby特有的功能之一.类用来表现具有数据与行为(程序)的"东西", 而模块大致来说,则是只有程序部分的集合体.类与模块最大的不同在于: 1.模块 ...
- Android开发周报:反编译对抗研究、动手制作智能镜子
新闻 <Android Wear落地中国 谷歌增强安卓生态控制力> :9月8日,由摩托罗拉推出的智能手表Moto 360二代作为国内发售的第一款搭载官方Android Wear的设备,正式 ...
- 4.windows和Linux下创建oracleusername表空间,表,插入数据,用户管理表等操作
进入超级管理员,运行下面命令 Window下创建数据库.表空间,用户,插入数据等操作 -- 01 创建表空间 -- 注意表空间的路径 依据实际安装环境进行调整 CREATE TABLESPACE ts ...
- html练习(5)
这个练习主要简单的展示了据对定位和相对定位: 在此说下html的定位: 1.static定位 这个是默认的方式.对static而言.left和right是不生效的. 2.relative定位(相对定位 ...