力扣算法题—146LRU缓存机制
【题目】
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。
获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。
写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。
进阶:
你是否可以在 O(1) 时间复杂度内完成这两种操作?
示例:
LRUCache cache = new LRUCache( 2 /* 缓存容量 */ );
cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // 返回 1
cache.put(3, 3); // 该操作会使得密钥 2 作废
cache.get(2); // 返回 -1 (未找到)
cache.put(4, 4); // 该操作会使得密钥 1 作废
cache.get(1); // 返回 -1 (未找到)
cache.get(3); // 返回 3
cache.get(4); // 返回 4
【解题思路】
见博客:左神算法进阶班5_4设计可以变更的缓存结构(LRU)
【代码】
class LRUCache {
public:
LRUCache(int capacity) {
this->capacity = capacity;
}
int get(int key) {
if (map.find(key) == map.end())
return -;
Node* p = map[key];
update(p);//更新使用频率
return p->val;
}
void put(int key, int value) {
if (map.find(key) == map.end())//不存在就存入
{
if (this->map.size() == this->capacity)//缓存空间已满
{
Node* p = this->head->next;
this->head->next = p->next;//删除位于链表头部的最不常用的节点
if (p->next == nullptr)//只有一个数据
end = head;
else
p->next->pre = head;
map.erase(p->key);//从表中删除,以留出空间
delete p;
}
Node* p = new Node(key, value);//新插入的数据在链表尾
this->end->next = p;
p->pre = end;
end = p;
map[key] = p;//存入数据
}
else//存在,但要更新数的使用频率
{
Node* p = map[key];//得到在链表中的位置
p->val = value;//更新数据值
update(p);//更新使用频率
}
}
private:
struct Node
{
int key;
int val;
Node* pre;
Node* next;
Node(int k, int v) :key(k), val(v), pre(nullptr), next(nullptr) {}
};
int capacity = ;
hash_map<int, Node*>map;
Node* head = new Node(' ', -);//指向链表的头
Node* end = head;//指向链表的尾
void update(Node* p)
{
if (p == end)
return;//p在链表尾部就不用移动了
Node* q = p->pre;
q->next = p->next;
p->next->pre = q;
end->next = p;
p->pre = end;//更新p的使用率,并挪至链表尾部
end = p;
}
};
/**
* 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);
*/
void Test()
{
LRUCache* rr = new LRUCache();
rr->put(, );
cout << rr->get() << endl;
rr->put(, );
cout << rr->get() << endl;
rr->get();
rr->put(, );
rr->get();
rr->get();
}
力扣算法题—146LRU缓存机制的更多相关文章
- 力扣算法题—460LFU缓存
[题目描述] 设计并实现最不经常使用(LFU)缓存的数据结构.它应该支持以下操作:get 和 put. get(key) - 如果键存在于缓存中,则获取键的值(总是正数),否则返回 -1. put(k ...
- 【力扣】146. LRU缓存机制
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - 如果关键字 (key) ...
- 力扣算法题—069x的平方根
实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...
- 力扣算法题—060第K个排列
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...
- 力扣算法题—050计算pow(x, n)
#include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...
- 力扣算法题—147Insertion_Sort_List
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- 力扣算法题—093复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...
- 力扣算法题—079单词搜索【DFS】
给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. ...
- 力扣算法题—052N皇后问题2
跟前面的N皇后问题没区别,还更简单 #include "000库函数.h" //使用回溯法 class Solution { public: int totalNQueens(in ...
随机推荐
- nginx基础内容
1.配置文件结构图 2.作用1:静态文件服务器 http { server { listen ; location / { root /data/www; } location /images/ { ...
- flutter 底部bottomNavigationBar凸起效果
概要 最近在做flutter 的时候,之前看到想实现 底部导航栏中间按钮 凸起效果, 最近想做又突然找不到方案了,因此记录下这里的实现方式. 预览效果 代码 主要使用 BottomAppBar 组建, ...
- ARM GNU 专有符号
1. @ 表示注释从当前位置到行尾的字符. 2. # 注释掉一整行. 3. ; 新行分隔符.
- Extremely fast hash algorithm-xxHash
xxHash - Extremely fast hash algorithm xxHash is an Extremely fast Hash algorithm, running at RAM sp ...
- adb命令 查看运行APP当前页面的Activity名称
命令 adb shell "dumpsys window | grep mCurrentFocus" 结果
- yii2 返回json和文件下载
JSON 調用的控制器返回 json格式的數據即可,對json裡面的數據沒有要求 如在控制器中添加一個方法: public function actionRemoveImage($id){ Yii:: ...
- Ionic POST提交使用普通表单提交数据
使用 和 GET 拼接参数一样拼接 doLogin() { let url = "http://loginApiUrl"; var headers = new Headers() ...
- 阿里云宣布进入 Serverless 容器时代,推出弹性容器实例服务 ECI
摘要: 阿里云宣布弹性容器实例 ECI(Elastic Container Instance)正式商业化. 为了应对业务高峰,打算提前多久执行ECS扩展?买了ECS虚拟机,容器规格不能完美装箱怎么办? ...
- C++——友元函数和友元类
友元函数:让函数可以访问类的私有属性 #include <iostream> using namespace std; class A { public: friend class B;/ ...
- html5 js 监听网络在线与离线
<!doctype html> <html> <head> <meta http-equiv="content-type" content ...