[Leetcode] [JAVA] 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 LRU cache, which uses the policy "retire most recently unused items if full", requires a sequence where each inserted item is placed at the beginning of the sequence and the item being searched is also moved to the beginning of the sequence. If the upper limit is exceeded, the last item of the sequence is deleted. Lookup, the insert operation requires an O (1) time complexity, otherwise the timeout will pass. So this sequence can be a list, and in order to find Fast, you need to combine HashMap. Because of the transfer of project operations, a doubly linked list is required.

HashMap is used to link the lookup key key to a specific project so that the project can be directly obtained and then moved to the top of the list.

It is important to note that in a set operation, if the set key already exists, it is equivalent to a find operation plus the operation to modify its value and HashMap.

The code is as follows:

1 classNode2     {3         intkey;4         intvalue;5 Node Next;6 Node Front;7Node (intKintv)8         {9key=K;TenValue=v; Onenext=NULL; AFront=NULL; -         } -     } the      -     PrivateHashmap<integer, node>HS; -     PrivateNode head; -     PrivateNode tail; +     Private intcap; -      PublicLRUCache (intcapacity) { +HS =NewHashmap<integer, node>(); AHead =NULL; atCap =capacity; -Tail =NULL; -     } -      -      Public intGetintkey) { -Node re =Hs.get (key); in         if(re==NULL) -             return-1; to         if(re.front==NULL) +             returnRe.value; -Re.front.next =Re.next; the         if(re.next!=NULL) *Re.next.front =Re.front; $         ElsePanax NotoginsengTail =Re.front; -Re.front =NULL; theRe.next =head; +Head.front =re; AHead =re; the         returnRe.value; +     } -      $      Public voidSetintKeyintvalue) { $Node temp =Hs.get (key); -         if(temp!=NULL) -         { the get (key); -Head.value =value;Wuyi Hs.put (key,head); the         } -         Else Wu         { -temp =NewNode (key, value); AboutTemp.next =head; $             if(head!=NULL) -Head.front =temp; -Head =temp; -             if(Hs.size () ==0) ATail =temp; + hs.put (key, temp); the             if(Hs.size () >cap) -             { $ Hs.remove (tail.key); theTail.front.next =NULL; theTail =Tail.front; the             } the         } -}

[Leetcode] [JAVA] 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.