Leetcode LRU Cache

Source: Internet
Author: User

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 ('ll 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 is reached its capacity, it should invalidate the least recently used item before inserting a new item.

The main thing is to consider the data structure, first we define a doubly linked list to save each node, and maintain a header and the end of the chain. At the same time, we use HashMap to store the index information of the node, storing the key and its corresponding node index.

Each time we execute the get and set methods, we place the node in the list header.

There are two things to consider when moving a node to a table header.

1. The node already exists in the list, and we need to delete the node in the linked list and add that node to the table header.

2. The node is new and is not in the list and is added directly to the header.

There are two scenarios to consider when executing the set (Key,value) method.

1. If the capacity is not exceeded, the node is directly new and placed on the table header.

2. In case of exceeding the capacity, first you need to delete the end of the chain, and then put the new node in the table header.

1  PackageLRU. Cache;2 3 ImportJava.util.HashMap;4 ImportJava.util.Map;5 6  Public classLRUCache {7Map <Integer,Node>map;8 Node head;9 Node tail;Ten     intLen; One     intnum; A     classnode{ -         intkey; -         intvalue; the Node Next; - Node pre; -          PublicNode (intKeyintvalue) { -              This. key=key; +              This. value=value; -              This. next=NULL; +              This. pre=NULL; A         } at     } -       PublicLRUCache (intcapacity) { -Map =NewHashmap<integer,node>(capacity); -Head=NewNode ( -1,-1); -Tail=NewNode ( -1,-1); -head.next=tail; inTail.pre=head; -len=capacity; toNum=0; +         } -          Public voidMovetohead (Node N) { the             if(n!=NULL){ *             if(Map.containskey (N.key)) { $Node headnext=Head.next;Panax NotoginsengNode npre=N.pre; -Node nnext=N.next; the             //Remove n +npre.next=NNext; ANnext.pre=Npre; the             } +             //put N to head -Node headnext1=Head.next; $n.next=HeadNext1; $Headnext1.pre=N; -head.next=N; -N.pre=head; the}Else return; -         }Wuyi          Public intRemoveTail () { theNode tailpre=Tail.pre; -Node prepre=Tailpre.pre; Wuprepre.next=tail; -Tail.pre=Prepre; About             returnTailpre.key; $         } -          Public intGetintkey) { -             if(Map.containskey (key)) { -Node obj=Map.get (key); A movetohead (obj); +                 returnObj.value; the}Else{ -                 return-1; $             } the         } the          the          Public voidSetintKeyintvalue) { the             if(Map.containskey (key)) { -Node obj=Map.get (key); inObj.value=value; the movetohead (obj); the}Else{ AboutNode obj=NewNode (key,value); the                 if(num<Len) { the movetohead (obj); the map.put (key, obj); +num++; -}Else{ the                     intRemove=RemoveTail ();Bayi Map.Remove (remove); the movetohead (obj); the map.put (key, obj); -                 } -             } the         } the}

Leetcode LRU Cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.