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 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.
Links: http://leetcode.com/problems/lru-cache/
5/14/2017
Preparing for an interview
With HashMap + double LinkedList implementation, HashMap key is key, value is a double linkedlist inside the node nodes
Note the issue:
1. It is best not to use the system deque to do double linkedlist, not easy to store node
2. Double linkedlist node includes key, value, Prev, next
3. You can compare the size and capacity of map directly without the need for additional size
4. Head, tail can be set to dummy node, simplifying the first judgment
5. Put contains get, you can omit some steps
6.31st, line 32 is to remove the old value of node from the double LinkedList, the equivalent of remove, and then to Riga only need to follow the general situation to be able to.
7. The 42nd line can be directly at Map.get (key). val = value to change the value of node, of course, the position of node in double LinkedList needs to be changed.
1 Public classLRUCache {2 classNode {3 intkey;4 intVal;5 Node prev;6 Node Next;7Node (intKeyintval) {8 This. Key =key;9 This. val =Val;Ten } One } AMap<integer, node>map; - Node head; - Node tail; the intcapacity; - - PublicLRUCache (intcapacity) { - This. Capacity =capacity; + This. Map =NewHashmap<integer, node>(); - This. Head =NewNode (-1,-1); + This. Tail =NewNode (-1,-1); A //Initialize Head/tail atTail.prev =head; -Head.next =tail; - } - - Public intGetintkey) { - if(Map.containskey (key)) { inNode old =Map.get (key); - //Why need these-treat old node as a new node similar idea as put remove the last node toOld.prev.next =Old.next; +Old.next.prev =Old.prev; - movetotail (old); the returnOld.val; *}Else { $ return-1;Panax Notoginseng } - } the + Public voidPutintKeyintvalue) { A if(Get (key)! =-1) { theMap.get (key). val =value; +}Else { - //Check Capacity First $ if(map.size () = = This. Capacity) { $ //Remove last node from map - Map.Remove (head.next.key); -Head.next =Head.next.next; theHead.next.prev =head; - }WuyiNode node =NewNode (key, value); the movetotail (node); - map.put (key, node); Wu } - } About Private voidMovetotail (node node) { $Node.prev =Tail.prev; -Tail.prev =node; -Node.prev.next =node; -Node.next =tail; A } + } the - /** $ * Your LRUCache object would be instantiated and called as such: the * LRUCache obj = new LRUCache (capacity); the * int param_1 = Obj.get (key); the * Obj.put (key,value); the */
Linkedhashmap
Https://discuss.leetcode.com/topic/43961/laziest-implementation-java-s-linkedhashmap-takes-care-of-everything
Https://discuss.leetcode.com/topic/17433/probably-the-best-java-solution-extend-linkedhashmap
More discussions
Https://discuss.leetcode.com/category/154/lru-cache
146. LRU Cache