LRU algorithm && Leetcode Problem Solving Report

Source: Internet
Author: User

Topic
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 T He key exists in the cache, otherwise return-1.set (key, value)-set or insert the value if the key is not already presen T. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

The LRU cache LRU is the abbreviation for the least recent used, which is the least-used cache replacement algorithm. is for virtual page Storage Management Services. The cache is fast cached. This is a concept that is often seen in the IT industry.    Caches in the CPU can greatly increase the time of access to instructions and data, allowing the entire memory (cache+ memory) to have both Cache speed and large memory capacity. Although the cache is fast, it has limited capacity. Therefore, when the cache capacity is exhausted and new content is added, it is necessary to select some content in the cache to discard, and then add the new content. The LRU cache substitution rule is to discard the content that was not used for the longest time, and then add the new content. Before the operating system, the teacher told me that LRU Cache algorithm Chinese translation is the longest unused algorithm in recent years.
The typical implementation of the idea LRU is double linked list + hash map.

The principle is:

    1. The two-way list is stored in an orderly manner based on the time of each node's recent visit, and the recently visited node is stored in the table header, which is stored on the last node that has not been interviewed, and is due to the fact that the recently visited node has a very large probability of being visited again in the next period of time.

    2. The function of a hash table is to improve the efficiency of lookups, assuming that a hash table is not used. Then the time complexity of finding a node is O (n). With a hash table, the lookup time complexity for each node is O (1).
Find (GET) operation:
    • Find HashMap based on key value, assuming no direct return-1
    • If node is found, it is inserted into the doubly linked list header
    • Returns the value of node
Insert (SET) operation:
    • Find HashMap based on key values. Suppose to find. Move the node directly to the table header to
    • Suppose not found. First infer whether the current cache is full
    • If it is full, delete the footer node
    • Inserting a new node into a table header

AC Code AC code, such as the following, write a more chaotic, in fact, a lot of methods can be concise reuse, but here is not changed, is to let everyone better clear the process and principle of LRU:
Import Java.util.hashmap;public class LRUCache {private Hashmap<integer, doublelistnode> mhashmap;private Doublelistnode head;private doublelistnode tail;private int capacity;private int currentsize;public LRUCache (int capacity) {this.capacity = Capacity;this.currentsize = 0;this.mhashmap = new Hashmap<integer, DoubleListNode> (); This.head = This.tail = null;} public int get (int key) {if (Mhashmap.containskey (key)) {Doublelistnode tnode = Mhashmap.get (key); if (tnode = = tail) {if ( CurrentSize > 1) {removenodefromtail (); Movenodetohead (tnode);}} else if (tnode = = head) {//Do nothing} else {tNode.pre.next = Tnode.next;tnode.next.pre = Tnode.pre;movenodetohead (tnode) ;} return Mhashmap.get (key). Value;} else {return-1;}} private void Removenodefromtail () {tail = tail.pre;if (tail! = null) {Tail.next = null;}} private void Movenodetohead (Doublelistnode node) {head.pre = Node;node.next = Head;node.pre = Null;head = node;} public void set (int key, int value) {if (Mhashmap.containskey (Key) {//update the corresponding value in HashMap and move the corresponding node of key to Team header Doublelistnode tnode = Mhashmap.get (key); tnode.value = value;if (tnode = tail) {if (CurrentSize > 1) {removenodefromtail (); Movenodetohead (tnode);}} else if (tnode = = head) {//Do nothing} else {tNode.pre.next = Tnode.next;tnode.next.pre = Tnode.pre;movenodetohead (tnode) ;} Mhashmap.put (key, tnode);} else {doublelistnode node = new Doublelistnode (key, value), Mhashmap.put (Key, node), if (currentsize = = 0) {head = Tail = no De;currentsize + = 1;} else if (CurrentSize < capacity) {Movenodetohead (node); currentsize + = 1;} else {//delete tail node. and add a Head node Mhashmap.remove (tail.key); Removenodefromtail ();//Add Header Node Movenodetohead (node);}}} public static void Main (string[] args) {LRUCache LRUCache = new LRUCache (1); Lrucache.set (2, 1); System.out.println (Lrucache.get (2)); Lrucache.set (3, 2); System.out.println (Lrucache.get (2)); System.out.println (Lrucache.get (3));} private static class Doublelistnode {public doublelistnode pre;public doublelistnode next;public int key;public int value;public doublelistnode (int key, int value) {This.key = Key;this.value = Value;this.pre = This.next = nul l;}}}

LRU Algorithm &amp;&amp; Leetcode Problem Solving report

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.