# Idiot-maker

:: :: :: :: :: :: :: ::
 291 随笔 :: 0 文章 :: 20 评论 :: 0 引用

https://leetcode.com/problems/lru-cache/

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.

public class LRUCache {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
LinkedList<Integer> list = new LinkedList<Integer>();
int capacity;

public LRUCache(int capacity) {
this.capacity = capacity;
}

public int get(int key) {
if(map.containsKey(key)) {
list.remove(new Integer(key));
list.add(key);
return map.get(key);
} else {
return -1;
}
}

public void set(int key, int value) {
if(list.size() < capacity) {
if(map.containsKey(key)) {
list.remove(new Integer(key));
list.add(key);
map.put(key, value);
} else {
list.add(key);
map.put(key, value);
}
} else {
if(map.containsKey(key)) {
list.remove(new Integer(key));
list.add(key);
map.put(key, value);
} else {
map.remove(list.get(0));
list.remove(0);
list.add(key);
map.put(key, value);
}
}
}
}

public class LRUCache {
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}

//保存真正键值对
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
//保存一个key在链表中的前一个节点
Map<Integer, ListNode> nodeMap = new HashMap<Integer, ListNode>();
//dummy节点，时刻位于链表的首位
ListNode dummy = new ListNode(0);
//lastNode时候指向链表的最后一个节点，主要为了在它后面add节点用
ListNode lastNode = dummy;
int capacity;

public LRUCache(int capacity) {
this.capacity = capacity;
}

public int get(int key) {
if(map.containsKey(key)) {
if(lastNode.val != key) {
ListNode pre = nodeMap.get(key);
nodeMap.put(pre.next.next.val, pre);
lastNode.next = pre.next;
pre.next = pre.next.next;
lastNode.next.next = null;
nodeMap.put(key, lastNode);
lastNode = lastNode.next;
}
return map.get(key);
} else {
return -1;
}
}

public void set(int key, int value) {
if(map.size() < capacity) {
ListNode pre = nodeMap.get(key);

ListNode next = new ListNode(key);
lastNode.next = next;
nodeMap.put(key, lastNode);
lastNode = next;

if(map.containsKey(key)) {
nodeMap.put(pre.next.next.val, pre);
pre.next = pre.next.next;
}
map.put(key, value);
} else {
ListNode pre = nodeMap.get(key);

ListNode next = new ListNode(key);
lastNode.next = next;
nodeMap.put(key, lastNode);
lastNode = next;

if(map.containsKey(key)) {
nodeMap.put(pre.next.next.val, pre);
pre.next = pre.next.next;
} else {
nodeMap.remove(new Integer(dummy.next.val));
map.remove(new Integer(dummy.next.val));
nodeMap.put(dummy.next.next.val, dummy);
dummy.next = dummy.next.next;
}
map.put(key, value);
}
}
}

update

http://www.blogjava.net/killme2008/archive/2010/09/16/332168.html

http://xiaozu.renren.com/xiaozu/100134/331695692

http://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist

http://www.exceptionhelp.com/javadetail?articleId=551

public class LRUCache {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
List<Integer> list = new ArrayList<Integer>();
int capacity;

public LRUCache(int capacity) {
this.capacity = capacity;
}

public int get(int key) {
if(map.containsKey(key)) {
list.remove(new Integer(key));
list.add(key);
return map.get(key);
} else {
return -1;
}
}

public void set(int key, int value) {
if(list.size() < capacity) {
if(map.containsKey(key)) {
list.remove(new Integer(key));
list.add(key);
map.put(key, value);
} else {
list.add(key);
map.put(key, value);
}
} else {
if(map.containsKey(key)) {
list.remove(new Integer(key));
list.add(key);
map.put(key, value);
} else {
map.remove(list.get(0));
list.remove(0);
list.add(key);
map.put(key, value);
}
}
}
}

posted on 2015-04-27 15:13 NickyYe 阅读(...) 评论(...) 编辑 收藏