当前位置:网站首页>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);
}今天的题目就写了这些。
边栏推荐
- Leetcode 1385. Distance value between two arrays
- High performance and high availability computing architecture of "microblog comments" in microblog system
- Matplotlib histogram of Matplotlib visualization plt bar()
- Software testing interface testing JMeter 5.5 installation tutorial
- Is it safe to open a stock account? Shanghai stock account opening.
- matplotlib matplotlib可视化之柱状图plt.bar()
- 远程沟通高效的自我总结| 社区征文
- 二叉搜索树 230. 二叉搜索树中第K小的元素 1038. 从二叉搜索树到更大和树
- LC:最大子数组和
- 声网自研传输层协议 AUT 的落地实践丨Dev for Dev 专栏
猜你喜欢

redis客户端

小程序插件接入、开发与注意事项

Start harvesting! Nailing: adjust the maximum number of free users of "nailing team". If the number exceeds 10, it will not work normally

Why is JSX syntax so popular?

机器学习:VC维的概念和用途

Leetcode 1385. 两个数组间的距离值

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

FPGA开发(2)——IIC通信

FPGA开发(1)——串口通信

HPE launched ARM CPU general server product
随机推荐
软件测试 接口测试 Postman测试工具 接口测试的流程 执行接口测试 接口关联 环境变量和全局变量 内置动态参数以及自动有的动态参数
Under the epidemic, I left my job for a year, and my income increased 10 times
Pain points and solutions of M1 notebook home office | community essay solicitation
Solr基础操作1
优雅的改造短信业务模块,策略模式走起!
Project 1 - buffer pool [cmu 15-445645] notes
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. Distance value between two arrays
Xutils3 transfer set
招商证券靠谱吗?开股票账户安全吗?
正则表达式:字符(2)
Matplotlib histogram of Matplotlib visualization plt bar()
2022 PMP project management examination agile knowledge points (5)
333333333333333333333333333333
Wechat applet: (update) cloud development wechat group contacts
sql刷题595. 大的国家
C指针进阶1-->字符指针,数组指针,指针与数组传参,函数指针
开始“收割”!钉钉调整“钉钉Teambition”免费用人数上限,超十人将无法正常用
海外数字身份验证服务商ADVANCE.AI入选EqualOcean《2022品牌出海服务市场研究报告》