import java.util.HashMap;
/*
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(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.
Follow up:
Could you do both operations in O(1) time complexity?
LRUCache cache = new LRUCache(2);
cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // returns 1
cache.put(3, 3); // evicts key 2
cache.get(2); // returns -1 (not found)
cache.put(4, 4); // evicts key 1
cache.get(1); // returns -1 (not found)
cache.get(3); // returns 3
cache.get(4); // returns 4
*/
class Node{//雙向鏈表
int key;
int value;
Node next;
Node pre;
public Node(int key,int value,Node pre, Node next){
this.key = key;
this.value = value;
this.pre = pre;
this.next = next;
}
}
public class LRUCache {
int capacity;
int count;//cache size
Node head;
Node tail;
HashMap<Integer,Node>hm;
public LRUCache(int capacity) { //only initial 2 Node is enough, head/tail
this.capacity = capacity;
this.count = 2;
this.head = new Node(-1,-1,null,null);
this.tail = new Node(-2,-2,this.head,null);
this.head.next = this.tail;
hm = new HashMap<Integer,Node>();
hm.put(this.head.key, this.head);
hm.put(this.tail.key, this.tail);
}
public int get(int key) {
int value = -1;
if(hm.containsKey(key)){
Node nd = hm.get(key);
value = nd.value;
detachNode(nd); //detach nd from current place
insertToHead(nd); //insert nd into head
}
return value;
}
public void put(int key, int value) {
if(hm.containsKey(key)){ //update
Node nd = hm.get(key);
nd.value = value;
//move to head
detachNode(nd); //detach nd from current place
insertToHead(nd); //insert nd into head
}else{ //add
Node newNd = new Node(key,value,null,this.head);
this.head.pre = newNd; //insert into head
this.head = newNd;
hm.put(key, newNd); //add into hashMap
this.count ++;
if(this.count > capacity){ //need delete node
removeNode();
}
}
}
//common func
public void insertToHead(Node nd){//最後結果爲B->C->A
//A->C->B,當前節點爲C,頭節點爲A
//頭節點的前一節點插入...........
this.head.pre = nd;
nd.next = this.head;
nd.pre = null;
this.head = nd;
}
public void detachNode(Node nd){
//當前節點爲nd,頭節點爲head
//比如 A->B->C,頭節點爲A,當前節點爲B
//當前節點的前一節點指向當前節點的下一節點 A->C
nd.pre.next = nd.next;
if(nd.next!=null){
//若當前節點的下一節點存在,則當前節點的下一節點的前一節點爲當前節點的前一節點
nd.next.pre = nd.pre;
//否則,尾節點爲當前節點的前一節點,即A->C->B
}else{
this.tail = nd.pre;
}
}
public void removeNode(){ //remove from tail
int tailKey = this.tail.key;
this.tail = this.tail.pre;
this.tail.next = null;
hm.remove(tailKey);
this.count --;
}
public void printCache(){
System.out.println("\nPRINT CACHE ------ ");
System.out.println("count: "+count);
System.out.println("From head:");
Node p = this.head;
while(p!=null){
System.out.println("key: "+p.key+" value: "+p.value);
p = p.next;
}
System.out.println("From tail:");
p = this.tail;
while(p!=null){
System.out.println("key: "+p.key+" value: "+p.value);
p = p.pre;
}
}
public static void main(String[] args){
LRUCache lc = new LRUCache(3);
System.out.println("LRUCache初始化狀態,Cache初始容量是2");
lc.printCache();
lc.put(1, 1);
lc.put(2, 2);
lc.put(3, 3);
System.out.println("第一次put操作後的結果");
lc.printCache();
lc.get(2);
System.out.println("第一次get(key==2)操作後的結果");
lc.printCache();
lc.put(4, 4);
System.out.println("第二次put(key==4)操作後的結果,容量已滿,替換最不常用的key和value");
lc.printCache();
lc.get(1);
System.out.println("第二次get(key==1)操作後的結果");
lc.printCache();
lc.put(3, 33);
System.out.println("第三次put(key==3)操作後的結果,替換已有key的值");
lc.printCache();
}
}
雙向鏈表:
class Node{
int key;
int value;
Node next;//下一節點
Node pre;//前一節點
public Node(int key,int value,Node pre, Node next){
this.key = key;
this.value = value;
this.pre = pre;
this.next = next;
}
}
圖解結構如下,
插入:假設要在key=1和key=2之間插入一個節點key=x
則,
Node k1,k2,kx;
已知k2,kx
kx.next = k2;
kx.pre = k2.pre;
k2.pre.next = kx;
----------------------------------
private Node head = new Node(null);
private void addBefore(Node newNode, Node node){
newNode.next = node;
newNode.pre = node.pre;
Node.pre.next = newNode;
}
//如有錯誤,請指出,待修改