当前位置:网站首页>6.29 problem solving
6.29 problem solving
2022-06-29 23:43:00 【After all, I still walk alone】
I wrote two topics today .
The first topic is difficult .

The title is the above .
The meaning of the topic is to design three kinds of functions . Then output the function type you want to use and the input value .
Then we can do different operations , Get the answer .
The first function is to set the size of the cache . A number to find out if it exists in the cache , If it exists, input its value . If it doesn't exist, negative one will be output .
The third is to add and replace the key value pair if it exists . If it does not exist, add it . Then, if the capacity has been exceeded , Delete the key value pairs that have not been used for the longest time
Then the big idea of this problem is to solve it with a hash table and a two-way linked list .
The hash table is used to store key value pairs A two-way linked list is used as a container
The addition operation is to take the data to be added as a new header node .
For the replaced data, put , After this data is deleted , Then add it . In this way, he will be ranked in the head node . That is to put his priority higher .
Then delete the tail node . That is, point to an empty node . The difficulty of this topic does not lie in the thinking , It may be how to write a complete hash table and a two-way linked list . Data structure .
struct DLinkedNode {
int key, value;
DLinkedNode* prev;// Connect to the previous node
DLinkedNode* next;// Connect to the next node
DLinkedNode(): key(0), value(0), prev(nullptr), next(nullptr) {}
DLinkedNode(int _key, int _value): key(_key), value(_value), prev(nullptr), next(nullptr) {}
};
class LRUCache {
private:
unordered_map<int, DLinkedNode*> cache;
DLinkedNode* head;
DLinkedNode* tail;
int size;
int capacity;
public:
LRUCache(int _capacity): capacity(_capacity), size(0) {
// Use head and tail nodes
head = new DLinkedNode();
tail = new DLinkedNode();
head->next = tail;
tail->prev = head;
}
int get(int key) {
if (!cache.count(key)) {
return -1;
}
DLinkedNode* node = cache[key];
moveToHead(node);// For mobile , Move to the head
return node->value;
}
void put(int key, int value) {
if (!cache.count(key)) {
// If key non-existent , Create a new node
DLinkedNode* node = new DLinkedNode(key, value);
// Add to hash table
cache[key] = node;
// Add to the head of the bidirectional list
addToHead(node);
++size;
if (size > capacity) {
// If the capacity is exceeded , Delete the tail node of the bidirectional linked list
DLinkedNode* removed = removeTail();
// Delete the corresponding entry in the hash table
cache.erase(removed->key);
delete removed;
--size;
}
}
else {
// If key There is , First, locate through the hash table , Revise value, And move to the head
DLinkedNode* node = cache[key];
node->value = value;
moveToHead(node);
}
}
void addToHead(DLinkedNode* node) {// Add a node
node->prev = head;
node->next = head->next;
head->next->prev = node;
head->next = node;
}
void removeNode(DLinkedNode* node) {// Delete a node
node->prev->next = node->next;
node->next->prev = node->prev;
}
void moveToHead(DLinkedNode* node) {
removeNode(node);
addToHead(node);// The requirement to move to the head is accomplished by deleting and adding once at a time
}
DLinkedNode* removeTail() {// To delete the tail node
DLinkedNode* node = tail->prev;
removeNode(node);
return node;
}
};That's the code .
Then there is another question .

There is nothing to say about this topic . If used sort Maybe it's just a few lines . But a fast platoon can also solve this problem if it is not used . It is to sort and then output the specified Reciprocal k The data of one location is enough .
The code is the following .
inline int partition(int* a, int l, int r) {
int x = a[r], i = l - 1;
for (int j = l; j < r; ++j) {
if (a[j] <= x) {
int t = a[++i];
a[i] = a[j], a[j] = t;
}
}
int t = a[i + 1];
a[i + 1] = a[r], a[r] = t;
return i + 1;
}
inline int randomPartition(int* a, int l, int r) {
int i = rand() % (r - l + 1) + l;
int t = a[i];
a[i] = a[r], a[r] = t;
return partition(a, l, r);
}
int quickSelect(int* a, int l, int r, int index) {
int q = randomPartition(a, l, r);
if (q == index) {
return a[q];
} else {
if(q < index){
return quickSelect(a, q + 1, r, index);
}else{
return quickSelect(a, l, q - 1, index);
}
}
}
int findKthLargest(int* nums, int numsSize, int k) {
srand(time(0));
return quickSelect(nums, 0, numsSize - 1, numsSize - k);
}That's all for today's topic .
边栏推荐
- Why is JSX syntax so popular?
- 雲和恩墨蓋國强,識別它、抓住它,在國產數據庫沸騰以前
- 剑指 Offer 14- II. 剪绳子 II
- 基金的估值,费用,会计核算
- Yunhe enmo, gaiguoqiang, identify it and grasp it before the domestic database boils
- Collection! Have you ever used these tools to improve programmer productivity?
- Leetcode 1385. 两个数组间的距离值
- 设置安全组、域名备案、申请ssl证书
- Head pressing Amway good-looking and practical dispensing machine SolidWorks model material here
- Xutils3 transfer set
猜你喜欢

HPE launched ARM CPU general server product

海外数字身份验证服务商ADVANCE.AI入选EqualOcean《2022品牌出海服务市场研究报告》

剑指 Offer 14- II. 剪绳子 II

Leetcode(680)——验证回文字符串 Ⅱ

记一次排查线上MySQL死锁过程,不能只会curd,还要知道加锁原理

Procurement intelligence is about to break out, and the "3+2" system of Alipay helps enterprises build core competitive advantages

为什么 JSX 语法这么香?

Wechat applet: big red festive UI guessing lantern riddles is also called guessing character riddles

软件测试 接口测试 Postman测试工具 接口测试的流程 执行接口测试 接口关联 环境变量和全局变量 内置动态参数以及自动有的动态参数

After working in the software development industry for six years, I changed my ideas in those years
随机推荐
@Scheduled注解的坑,我替你踩了
Solr基础操作4
收藏!这些提高程序员生产力的工具你用过吗?
剑指 Offer 14- I. 剪绳子
SQL question brushing 595 Big country
正则表达式:字符(2)
手机开户一般哪个证券公司好?另外,手机开户安全么?
Implementation of aut, a self-developed transport layer protocol for sound network -- dev for dev column
MetaQ集群安装测试
Applet plug-in access, development and precautions
Cacti最大监控数测试
LC:最大子数组和
Solr basic operation 5
按头安利 好看又实用的点胶机 SolidWorks模型素材看这里
二叉搜索树 230. 二叉搜索树中第K小的元素 1038. 从二叉搜索树到更大和树
matplotlib matplotlib可视化之柱状图plt.bar()
Start harvesting! Nailing: adjust the maximum number of free users of "nailing team". If the number exceeds 10, it will not work normally
RRDTOOL draws MRTG log data
除子
Leetcode(633)——平方数之和