classNode:def__init__(self,key:int=0,val:int=0):self.key=keyself.val=valself.prev=Noneself.next=NoneclassLRUCache:def__init__(self,capacity:int):self.size=0self.capacity=capacityself.cache={}self.head=Node()self.tail=Node()self.head.next=self.tailself.tail.prev=self.headdefget(self,key:int)->int:ifkeynotinself.cache:return-1node=self.cache[key]self.remove_node(node)self.add_to_head(node)returnnode.valdefput(self,key:int,value:int)->None:ifkeyinself.cache:node=self.cache[key]self.remove_node(node)node.val=valueself.add_to_head(node)else:node=Node(key,value)self.cache[key]=nodeself.add_to_head(node)self.size+=1ifself.size>self.capacity:node=self.tail.prevself.cache.pop(node.key)self.remove_node(node)self.size-=1defremove_node(self,node):node.prev.next=node.nextnode.next.prev=node.prevdefadd_to_head(self,node):node.next=self.head.nextnode.prev=self.headself.head.next=nodenode.next.prev=node# Your LRUCache object will be instantiated and called as such:# obj = LRUCache(capacity)# param_1 = obj.get(key)# obj.put(key,value)
classNode{intkey,val;Nodeprev,next;Node(){}Node(intkey,intval){this.key=key;this.val=val;}}classLRUCache{privateintsize;privateintcapacity;privateNodehead=newNode();privateNodetail=newNode();privateMap<Integer,Node>cache=newHashMap<>();publicLRUCache(intcapacity){this.capacity=capacity;head.next=tail;tail.prev=head;}publicintget(intkey){if(!cache.containsKey(key)){return-1;}Nodenode=cache.get(key);removeNode(node);addToHead(node);returnnode.val;}publicvoidput(intkey,intvalue){if(cache.containsKey(key)){Nodenode=cache.get(key);removeNode(node);node.val=value;addToHead(node);}else{Nodenode=newNode(key,value);cache.put(key,node);addToHead(node);if(++size>capacity){node=tail.prev;cache.remove(node.key);removeNode(node);--size;}}}privatevoidremoveNode(Nodenode){node.prev.next=node.next;node.next.prev=node.prev;}privatevoidaddToHead(Nodenode){node.next=head.next;node.prev=head;head.next=node;node.next.prev=node;}}/** * Your LRUCache object will be instantiated and called as such: * LRUCache obj = new LRUCache(capacity); * int param_1 = obj.get(key); * obj.put(key,value); */
classLRUCache{private:structNode{intkey,val;Node*prev;Node*next;Node(intkey,intval):key(key),val(val),prev(nullptr),next(nullptr){}};intsize;intcapacity;Node*head;Node*tail;unordered_map<int,Node*>cache;voidremoveNode(Node*node){node->prev->next=node->next;node->next->prev=node->prev;}voidaddToHead(Node*node){node->next=head->next;node->prev=head;head->next->prev=node;head->next=node;}public:LRUCache(intcapacity):size(0),capacity(capacity){head=newNode(0,0);tail=newNode(0,0);head->next=tail;tail->prev=head;}intget(intkey){if(!cache.contains(key)){return-1;}Node*node=cache[key];removeNode(node);addToHead(node);returnnode->val;}voidput(intkey,intvalue){if(cache.contains(key)){Node*node=cache[key];removeNode(node);node->val=value;addToHead(node);}else{Node*node=newNode(key,value);cache[key]=node;addToHead(node);if(++size>capacity){node=tail->prev;cache.erase(node->key);removeNode(node);--size;}}}};/** * Your LRUCache object will be instantiated and called as such: * LRUCache* obj = new LRUCache(capacity); * int param_1 = obj->get(key); * obj->put(key,value); */
typeNodestruct{key,valintprev,next*Node}typeLRUCachestruct{size,capacityinthead,tail*Nodecachemap[int]*Node}funcConstructor(capacityint)LRUCache{head:=&Node{}tail:=&Node{}head.next=tailtail.prev=headreturnLRUCache{capacity:capacity,head:head,tail:tail,cache:make(map[int]*Node),}}func(this*LRUCache)Get(keyint)int{ifnode,exists:=this.cache[key];exists{this.removeNode(node)this.addToHead(node)returnnode.val}return-1}func(this*LRUCache)Put(keyint,valueint){ifnode,exists:=this.cache[key];exists{this.removeNode(node)node.val=valuethis.addToHead(node)}else{node:=&Node{key:key,val:value}this.cache[key]=nodethis.addToHead(node)ifthis.size++;this.size>this.capacity{node=this.tail.prevdelete(this.cache,node.key)this.removeNode(node)this.size--}}}func(this*LRUCache)removeNode(node*Node){node.prev.next=node.nextnode.next.prev=node.prev}func(this*LRUCache)addToHead(node*Node){node.next=this.head.nextnode.prev=this.headthis.head.next=nodenode.next.prev=node}/** * Your LRUCache object will be instantiated and called as such: * obj := Constructor(capacity); * param_1 := obj.Get(key); * obj.Put(key,value); *//** * Your LRUCache object will be instantiated and called as such: * obj := Constructor(capacity); * param_1 := obj.Get(key); * obj.Put(key,value); */
classNode{key:number;val:number;prev:Node|null;next:Node|null;constructor(key:number,val:number){this.key=key;this.val=val;this.prev=null;this.next=null;}}classLRUCache{privatesize:number;privatecapacity:number;privatehead:Node;privatetail:Node;privatecache:Map<number,Node>;constructor(capacity:number){this.size=0;this.capacity=capacity;this.head=newNode(0,0);this.tail=newNode(0,0);this.head.next=this.tail;this.tail.prev=this.head;this.cache=newMap();}get(key:number):number{if(!this.cache.has(key)){return-1;}constnode=this.cache.get(key)!;this.removeNode(node);this.addToHead(node);returnnode.val;}put(key:number,value:number):void{if(this.cache.has(key)){constnode=this.cache.get(key)!;this.removeNode(node);node.val=value;this.addToHead(node);}else{constnode=newNode(key,value);this.cache.set(key,node);this.addToHead(node);if(++this.size>this.capacity){constnodeToRemove=this.tail.prev!;this.cache.delete(nodeToRemove.key);this.removeNode(nodeToRemove);--this.size;}}}privateremoveNode(node:Node):void{if(!node)return;node.prev!.next=node.next;node.next!.prev=node.prev;}privateaddToHead(node:Node):void{node.next=this.head.next;node.prev=this.head;this.head.next!.prev=node;this.head.next=node;}}/** * Your LRUCache object will be instantiated and called as such: * var obj = new LRUCache(capacity) * var param_1 = obj.get(key) * obj.put(key,value) */
usestd::cell::RefCell;usestd::collections::HashMap;usestd::rc::Rc;structNode{key:i32,value:i32,prev:Option<Rc<RefCell<Node>>>,next:Option<Rc<RefCell<Node>>>,}implNode{#[inline]fnnew(key:i32,value:i32)->Self{Self{key,value,prev:None,next:None,}}}structLRUCache{capacity:usize,cache:HashMap<i32,Rc<RefCell<Node>>>,head:Option<Rc<RefCell<Node>>>,tail:Option<Rc<RefCell<Node>>>,}/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */implLRUCache{fnnew(capacity:i32)->Self{Self{capacity:capacityasusize,cache:HashMap::new(),head:None,tail:None,}}fnget(&mutself,key:i32)->i32{matchself.cache.get(&key){Some(node)=>{letnode=Rc::clone(node);self.remove(&node);self.push_front(&node);letvalue=node.borrow().value;value}None=>-1,}}fnput(&mutself,key:i32,value:i32){matchself.cache.get(&key){Some(node)=>{letnode=Rc::clone(node);node.borrow_mut().value=value;self.remove(&node);self.push_front(&node);}None=>{letnode=Rc::new(RefCell::new(Node::new(key,value)));self.cache.insert(key,Rc::clone(&node));self.push_front(&node);ifself.cache.len()>self.capacity{letback_key=self.pop_back().unwrap().borrow().key;self.cache.remove(&back_key);}}};}fnpush_front(&mutself,node:&Rc<RefCell<Node>>){matchself.head.take(){Some(head)=>{head.borrow_mut().prev=Some(Rc::clone(node));node.borrow_mut().prev=None;node.borrow_mut().next=Some(head);self.head=Some(Rc::clone(node));}None=>{self.head=Some(Rc::clone(node));self.tail=Some(Rc::clone(node));}};}fnremove(&mutself,node:&Rc<RefCell<Node>>){match(node.borrow().prev.as_ref(),node.borrow().next.as_ref()){(None,None)=>{self.head=None;self.tail=None;}(None,Some(next))=>{self.head=Some(Rc::clone(next));next.borrow_mut().prev=None;}(Some(prev),None)=>{self.tail=Some(Rc::clone(prev));prev.borrow_mut().next=None;}(Some(prev),Some(next))=>{next.borrow_mut().prev=Some(Rc::clone(prev));prev.borrow_mut().next=Some(Rc::clone(next));}};}fnpop_back(&mutself)->Option<Rc<RefCell<Node>>>{matchself.tail.take(){Some(tail)=>{self.remove(&tail);Some(tail)}None=>None,}}}
publicclassLRUCache{privateintsize;privateintcapacity;privateDictionary<int,Node>cache=newDictionary<int,Node>();privateNodehead=newNode();privateNodetail=newNode();publicLRUCache(intcapacity){this.capacity=capacity;head.Next=tail;tail.Prev=head;}publicintGet(intkey){if(!cache.ContainsKey(key)){return-1;}Nodenode=cache[key];RemoveNode(node);AddToHead(node);returnnode.Val;}publicvoidPut(intkey,intvalue){if(cache.ContainsKey(key)){Nodenode=cache[key];RemoveNode(node);node.Val=value;AddToHead(node);}else{Nodenode=newNode(key,value);cache[key]=node;AddToHead(node);if(++size>capacity){node=tail.Prev;cache.Remove(node.Key);RemoveNode(node);--size;}}}privatevoidRemoveNode(Nodenode){node.Prev.Next=node.Next;node.Next.Prev=node.Prev;}privatevoidAddToHead(Nodenode){node.Next=head.Next;node.Prev=head;head.Next=node;node.Next.Prev=node;}// Node class to represent each entry in the cache.privateclassNode{publicintKey;publicintVal;publicNodePrev;publicNodeNext;publicNode(){}publicNode(intkey,intval){Key=key;Val=val;}}}/** * Your LRUCache object will be instantiated and called as such: * LRUCache obj = new LRUCache(capacity); * int param_1 = obj.Get(key); * obj.Put(key,value); */