Huawei OD interview hand tearing code real questions [LRU implementation]

Handwritten LRU

        I still let leetcode write questions, but I feel that this question is biased towards students who have just left school. Generally, after working for a few years, LRU may have forgotten what it is.

Please design and implement a data structure that satisfies the LRU (least recently used) cache constraint.
Implement the LRUCache class:
LRUCache(int capacity) Use a positive integer as the capacity to initialize the LRU cache
int get(int key) If the key exists in the cache, return the value of the key, otherwise return -1.
void put(int key, int value) If the keyword key already exists, change its data value value; if it does not exist, insert the group key-value into the cache. If an insert operation causes the number of keywords to exceed capacity , the oldest unused keyword should be evicted.
The functions get and put must run with an average time complexity of O(1).

        leetcode original question ,

        First of all, let’s briefly introduce the LRU algorithm. If you are given this question in the interview, but you don’t know it, it’s best to change the question or ask the interviewer to give you a hint. If you don’t know it silently, no one will feel sorry for you in the end .        

        The LRU algorithm is a cache elimination strategy. The principle is not difficult, but it is more skillful to write an algorithm without bugs in the interview. It needs to abstract and disassemble the data structure layer by layer. This article will take you to write a beautiful code.

Guess you like

Origin blog.csdn.net/misayaaaaa/article/details/131161720