当前位置:网站首页>Hash table: least recently used cache
Hash table: least recently used cache
2022-06-13 02:45:00 【Zeng Qiang】
List of articles
background
The basis of hash table
Hash table is a more complex and efficient data structure , Include HashSet and HashMap, The time complexity of their query to delete an element is O(1). Often used to optimize time efficiency . Hash tables are efficient , But not everything , If you need to sort elements , Then use TreeMap.
Design of hash table
- Array : Design hash table based on array , The remainder of the hash value of the element to the length of the array is used as the array subscript , The time efficiency of finding elements is O(1).
- Linked list : When the hash values are equal , Array subscripts will cause conflicts , You need to store multiple elements in the same location of the array in a linked list .
- Capacity expansion : To ensure efficiency , When the ratio of the number of elements to the length of the array exceeds the threshold , We need to expand the length of the array .
subject
Please design and implement a least recently used cache , The time complexity of the following two operations is required to be O(1).
- get(key)
- put(key, value)
Their thinking
This problem requires us to design a cached data structure ,
- We have to record the order in which the nodes are accessed . If we use HashMap, Use get,put when , Time complexity can be achieved O(1), Visit one node at a time , Let's take the node's key Store as key hashMap in , Node most HashMap Value storage for , At the same time, the node is added to the end of the linked list , In this way, the head node of the linked list is the least used node , The tail node is the newly used node .
- Two way linked list replaces one-way linked list . Enables you to quickly delete and query nodes .
Something to watch out for :
- When the number of nodes exceeds the threshold , The least recently used nodes need to be eliminated .
- With the help of sentry node , Easy to update cache .
The specific logic code is as follows :
Code
class LRUCache {
private int capacity = 0;
private ListNode head;
private ListNode tail;
private Map<Integer, ListNode> cache;
public LRUCache(int capacity) {
this.capacity = capacity;// threshold
// Initialize cache hash table
cache = new HashMap<Integer, ListNode>();
// Initializing a double linked list , Here the sentry node is used to quickly add the tail node .
head = new ListNode(-1, -1);
tail = new ListNode(-1, -1);
head.next = tail;
tail.pre = head;
}
public int get(int key) {
ListNode node = cache.get(key);
if(node == null) {
return -1;
}
moveToTail(node, node.value);
return node.value;
}
public void put(int key, int value) {
if(cache.containsKey(key)) {
moveToTail(cache.get(key), value);
} else {
if(cache.size() == capacity) {
ListNode tobeDeleteNode = head.next;
deleteNode(tobeDeleteNode);
cache.remove(tobeDeleteNode.key);
}
ListNode node = new ListNode(key, value);
insertToTail(node);
cache.put(key, node);
}
}
private void deleteNode(ListNode node) {
node.pre.next = node.next;
node.next.pre = node.pre;
}
private void moveToTail(ListNode node, int value) {
//detete
deleteNode(node);
//insertToTail
node.value = value;
insertToTail(node);
}
private void insertToTail(ListNode node) {
node.pre = tail.pre;
tail.pre.next = node;
node.next = tail;
tail.pre = node;
}
class ListNode {
ListNode pre;
ListNode next;
int key;
int value;
public ListNode(int key, int value){
this.key = key;
this.value = value;
}
}
}
/** * Your LRUCache object will be instantiated and called as such: * LRUCache obj = new LRUCache(capacity); * int param_1 = obj.get(key); * obj.put(key,value); */
summary
This topic examines the knowledge points of hash table , It should be noted that the least recently used cache needs to record the order of element access , So the data structure of the final cache is designed as hash table and bidirectional linked list .
边栏推荐
- too old resource version,Code:410
- Ijkplayer source code ---packetqueue
- OpenCVSharpSample05Wpf
- Retrofit easy to use
- JS deconstruction assignment
- Node uses post to request req Pit with empty body
- Laravel permission export
- Introduction to facial expression recognition system - Technical Paper Edition
- Welcome to blog navigation
- How to select fund products? What kind of fund is a good fund?
猜你喜欢
![Leetcode 473. Match to square [violence + pruning]](/img/3a/975b91dd785e341c561804175b6439.png)
Leetcode 473. Match to square [violence + pruning]
![[data and Analysis Visualization] D3 introductory tutorial 2- building shapes in D3](/img/b8/06779a82e79e44534573fa60460b31.jpg)
[data and Analysis Visualization] D3 introductory tutorial 2- building shapes in D3
![HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)](/img/c9/884aa008a185a471dfe252c0756fc1.png)
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)

Detailed installation tutorial of MATLAB r2019 B-mode ultrasound (complete installation files are attached)

Image classification system based on support vector machine (Matlab GUI interface version)
![[data analysis and visualization] key points of data mapping 7- over mapping](/img/ae/d4e251b37ec4857c99f738ca981092.jpg)
[data analysis and visualization] key points of data mapping 7- over mapping

Special topic I of mathematical physics of the sprint strong foundation program

Ijkplayer source code ---packetqueue
![[reading papers] comparison of deeplobv1-v3 series, brief review](/img/80/714b8e5b2ad31b0a1a0b8320a3c714.jpg)
[reading papers] comparison of deeplobv1-v3 series, brief review

01 初识微信小程序
随机推荐
Opencvshare4 and vs2019 configuration
JS multiple judgment writing
[reading point paper] deeplobv3+ encoder decoder with Atlas separable revolution
数字IC设计——FIFO的设计
CDN single page reference of indexbar index column in vant framework cannot be displayed normally
Principle and steps of principal component analysis (PCA)
Ijkplayer source code --- decode
小程序 input,textarea组件权重比fixed的z-index都高
03 recognize the first view component
[deep learning] fast Reid tutorial
Graph theory, tree based concept
Implementing fillet in custom dialog
Resource arrangement
04路由跳转并携带参数
Vant realizes the adaptation of mobile terminal
Detailed explanation of data processing in machine learning (I) -- missing value processing (complete code attached)
Detailed explanation of handwritten numeral recognition based on support vector machine (Matlab GUI code, providing handwriting pad)
[reading papers] deep learning face representation from predicting 10000 classes. deepID
如何挑选基金产品?什么样的基金是好基金?
How to select fund products? What kind of fund is a good fund?