当前位置:网站首页>6.29日刷题题解
6.29日刷题题解
2022-06-29 23:38:00 【终究还是一人独行】
今天写了两个题目。
第一个题目比较难。

题目就是上面这种。
题目的意思就是设计三种函数。然后输出你要使用的函数类型和输入的值。
然后来进行不同的操作,得到答案。
第一个函数是设定缓存的大小。一个数查找它是否存在于这个缓存中,如果存在就把它的值输出来。不存在的话就输出负一。
第三个的话就是进行添加如果存在这个键值对就进行替换。 如果不存在就进行添加。然后如果已经超出容量范围的话,就将最久没有使用过的键值对进行删除
然后这个题的大题思路就是用一个哈希表和双向链表来进行解答。
哈希表的作用就是用来储存键值对 双向链表就是用来当做容器
对于添加的操作就是把要添加的数据作为新的头节点。
对于替换的数据就把,这个数据删除后,然后再进行添加。这样的话他就会被排在头节点。也就是让他的优先级排在更前面。
然后删除操作的话就是删除尾节点。也就是指向空的节点。 这个题目的难点不在于这个思维,可能就是怎么完整的写一个哈希表和双向链表结合起来的。数据结构吧。
struct DLinkedNode {
int key, value;
DLinkedNode* prev;//连接上一个节点
DLinkedNode* next;//连接下一个节点
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) {
// 使用头部和尾部节点
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);//进行移动,移动到头部
return node->value;
}
void put(int key, int value) {
if (!cache.count(key)) {
// 如果 key 不存在,创建一个新的节点
DLinkedNode* node = new DLinkedNode(key, value);
// 添加进哈希表
cache[key] = node;
// 添加至双向链表的头部
addToHead(node);
++size;
if (size > capacity) {
// 如果超出容量,删除双向链表的尾部节点
DLinkedNode* removed = removeTail();
// 删除哈希表中对应的项
cache.erase(removed->key);
delete removed;
--size;
}
}
else {
// 如果 key 存在,先通过哈希表定位,再修改 value,并移到头部
DLinkedNode* node = cache[key];
node->value = value;
moveToHead(node);
}
}
void addToHead(DLinkedNode* node) {//添加节点
node->prev = head;
node->next = head->next;
head->next->prev = node;
head->next = node;
}
void removeNode(DLinkedNode* node) {//删除一个节点
node->prev->next = node->next;
node->next->prev = node->prev;
}
void moveToHead(DLinkedNode* node) {
removeNode(node);
addToHead(node);//通过一次删除一次添加来完成移动到头部的要求
}
DLinkedNode* removeTail() {//进行删除尾部节点
DLinkedNode* node = tail->prev;
removeNode(node);
return node;
}
};代码就是上面这些。
然后就是另外一个题。

这个题目没什么可说的。如果使用了sort 可能就是几行的事情。但是不使用的话就是一个快排也可以解决这个问题。 就是排序后然后输出他指定的 倒数k个位置的数据就可以了.
代码就是下面这个。
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);
}今天的题目就写了这些。
边栏推荐
- Wechat applet: big red festive UI guessing lantern riddles is also called guessing character riddles
- 软件测试 接口测试 Postman测试工具 接口测试的流程 执行接口测试 接口关联 环境变量和全局变量 内置动态参数以及自动有的动态参数
- 2022 PMP project management examination agile knowledge points (5)
- flutter 插件版本冲突的解决方法
- Principe de réalisation de l'agent dynamique
- Cacti最大监控数测试
- Solr basic operation
- SQL question brushing 595 Big country
- 剑指 Offer 14- I. 剪绳子
- Solr基础操作
猜你喜欢

Gracefully transform the SMS business module and start the strategic mode!

CE second operation

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

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

Software testing interface testing postman testing tool interface testing process execution interface testing interface associated environment variables and global variables built-in dynamic parameter

采购数智化爆发在即,支出宝“3+2“体系助力企业打造核心竞争优势

Create an API rapid development platform, awesome!

二叉搜索树 230. 二叉搜索树中第K小的元素 1038. 从二叉搜索树到更大和树

Collection! Have you ever used these tools to improve programmer productivity?

FPGA开发(2)——IIC通信
随机推荐
Overseas digital authentication service provider advance AI was selected into the "2022 brand sea Service Market Research Report" of equalocean
25 interview questions about Apache
動態代理的實現原理
关于 Apache 的 25 个初中级面试题
数据库-玩转数据-Pgsql 使用UUID做主键
Halcon实用:焊点检出设计思路
Become the only key
Create an API rapid development platform, awesome!
均值、方差、标准差、协方差的概念及意义
redis客户端
C pointer advanced 2-- > function pointer array callback function simplifies calculator code, and implements qsort function based on callback function simulation
HPE launched ARM CPU general server product
After working in the software development industry for six years, I changed my ideas in those years
Metaq cluster installation test
LC:有效的数独 + 旋转图像
Welcome the "top ten" of the Municipal Association for science and technology • pay tribute to Lu Yi, a scientific and technological worker: an explorer guarding the transmission security of the power
“微博评论”的高性能高可用计算架构
Start harvesting! Nailing: adjust the maximum number of free users of "nailing team". If the number exceeds 10, it will not work normally
Leetcode 1385. 两个数组间的距离值
股票开户安全吗?上海股票开户。