[LeetCode] LRU Cache solution report, leetcodelru
Digress: I wrote several blog posts in a row, and my blog ranking is no longer "thousands of miles away". I have already reached the 20 thousand th place. Continue to work. Come on!
Design and implement a data structure for Least Recently Used (LRU) cache. It shocould support the following operations:get
Andset
.
get(key)
-Get the value (will 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 reached its capacity, it shocould invalidate the least recently used item before inserting a new item.
Question]
Design and implement a cache that supports get and set operations:
If get (key)-exists, its value is returned; otherwise,-1 is returned;
Set (key)-Insert a new value if it does not exist and update the value if it exists. Note that when the capacity is full, delete the key that has not been accessed for the longest time and delete it, and insert a new key.
=============================== Map + List Implementation Method ========================== =====
[Idea]
Use the map structure to store and read <key, value>.
A list is used to record the time when the key was accessed. recently accessed is placed at the end of the list. The first key in the list indicates that the key was not accessed for the longest time.
[Java code]
Class LRUCache {HashMap <Integer, Integer> map; ArrayList <Integer> list; int capacity; public LRUCache (int capacity) {map = new HashMap <Integer, Integer> (capacity ); list = new ArrayList <Integer> (capacity); this. capacity = capacity;} public int get (int key) {if (map. get (key) = null) return-1; list. remove (new Integer (key); list. add (key); return map. get (key);} public void set (int key, int value ) {If (map. get (key )! = Null) {// The original key map exists. put (key, value); list. remove (new Integer (key); list. add (key);} else {// key if (map. size () <capacity) {// The capacity is less than map. put (key, value); list. add (key);} else {// capacity full int leastkey = list. remove (0); list. add (key); map. remove (leastkey); map. put (key, value );}}}}
Note]
The question must be Least Recently Used. Not only do you need to update the list during set, but also update the list during get.
When you set a map, you must first judge whether the map is full or not. If the map is full, you must first judge whether the map is full and then whether the map has this value, in this case, it is wrong.
If this value exists when the map is full, update it directly. If you first determine whether the map is full, the maximum deletion time is not accessed.
[Conventional solution]
A two-way linked list is usually used to record the elements that have not been accessed for the longest time, because the two-way linked list can move a node to the header and delete the tail node within the O (1) time.
In the above Code, list is used. When removing a node, the entire list is traversed to find a node.
LeetCode does not have a requirement on the time. It will certainly be required during the interview.
Q: What is the LRU Cache in Java? It is best to explain in detail,
B
How to use c ++ to implement an lru cache
In fact, the key is not to use C ++ or anything.
Generally, a map and a two-way linked list can be used to implement lru.