力扣算法题—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 ...
随机推荐
- 使用idea从svn检出项目
1.创建空的工程 2. 3. 编辑项目进行提交svn服务器进行自动整合
- Response案例1_重定向
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.serv ...
- [转载]Spring AOP 深入剖析
转载自 http://www.cnblogs.com/digdeep/p/4528353.html 多谢@digdeep AOP是Spring提供的关键特性之一.AOP即面向切面编程,是OOP编程的有 ...
- Activiti学习笔记8 — UserTask私有任务的使用
每一个UserTask都会在Execution表和Task表中各产生一条记录 一.创建流程引擎对象 /** * 1.创建流程引擎对象 */ private ProcessEngine processE ...
- JAVA时间工具类用法
1.获得N天前的TIMESTAMP Calendar cl = Calendar.getInstance(); cl.add(Calendar.DAY_OF_YEAR, -7); Date date ...
- iOS进阶四-自动释放池原理
概述 AutoreleasePool(自动释放池)是OC中的一种内存自动回收机制,它可以延迟加入AutoreleasePool中的变量release的时机.在正常情况下,创建的变量会在超出其作用域的时 ...
- JS对象 四舍五入round() round() 方法可把一个数字四舍五入为最接近的整数。 语法: Math.round(x)
四舍五入round() round() 方法可把一个数字四舍五入为最接近的整数. 语法: Math.round(x) 参数说明: 注意: 1. 返回与 x 最接近的整数. 2. 对于 0.5,该方法将 ...
- MySQL入门基础知识
1.MySQL环境变量的配置 操作数据库时,要进入bin目录,如下: 但是如果进行配置环境变量,就不必切换路径,如下图所示,即使没有在G:\mysql-8.0.16-winx64\bin下,数据库依然 ...
- The linux command 之存储媒介
一.常用的命令 mount:挂载文件系统 unmount:卸载文件系统 fdisk:硬盘分区命令 fdformat:格式化软盘 fsck:检查和修复文件系统 mkfs:创建文件系统 dd:转换和拷贝一 ...
- CF 540D Bad Luck Island
一看就是DP题(很水的一道紫题) 设\(dp[i][j][k]\)为留下\(i\)个\(r\)族的人,死去\(j\)个\(s\)族的人,死去\(k\)个\(p\)族的人的概率(跟其他的题解有点差别,但 ...