文章目录
146. LRU 缓存机制
力扣原题
class Node{
public int key;
public int value;
public Node next;
public Node pre;
public Node(int key,int value){
this.key=key;
this.value=value;
}
}
class DoubleList{
private Node head, tail;
private int size;
public int size(){
return size;
}
public DoubleList( ){
head=new Node(0,0);
tail=new Node(0,0);
head.next=tail;
tail.pre=head;
size=0;
}
//删除
public void delete(Node x){
x.pre.next=x.next;
x.next.pre=x.pre;
size--;
}
//删除头部元素并返回
public Node deleteFirst(){
if(head.next==null){
return null;
}
Node tem=head.next;
head.next=tem.next;
tem.next.pre=head;
size--;
return tem;
}
//尾部添加
public void addLast(Node x){
tail.pre.next=x;
x.pre=tail.pre;
x.next=tail;
tail.pre=x;
size++;
}
}
class LRUCache {
private HashMap