problemscpp
A collection of my answers to algorithm problems in c++.
Public 成员函数 | Private 属性 | 所有成员列表
leetcode::lru_cache::LRUCache类 参考

#include <leetcode.h>

Public 成员函数

 LRUCache (int capacity)
 以 正整数 作为容量 capacity 初始化 LRU 缓存 更多...
 
int get (int key)
 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。 更多...
 
void put (int key, int value)
 如果关键字 key 已经存在,则变更其数据值 value ;如果不存在,则向缓存中插入该组 key-value 。如果插入操作导致关键字数量超过 capacity ,则应该 逐出 最久未使用的关键字。 更多...
 

Private 属性

int capacity
 
list< int > keys
 
unordered_map< int, int > um
 

详细描述

在文件 leetcode.h3284 行定义.

构造及析构函数说明

◆ LRUCache()

leetcode::lru_cache::LRUCache::LRUCache ( int  capacity)

以 正整数 作为容量 capacity 初始化 LRU 缓存

在文件 leetcode.cpp9372 行定义.

成员函数说明

◆ get()

int leetcode::lru_cache::LRUCache::get ( int  key)

如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。

在文件 leetcode.cpp9375 行定义.

9375 {
9376 if(um.contains(key)) {
9377 if(key != keys.back()) {
9378 keys.erase(find(keys.begin(), keys.end(), key));
9379 keys.emplace_back(key);
9380 }
9381 return um[key];
9382 }
9383 return -1;
9384 }
bool find(vector< unordered_set< int > > &g, int x, vector< bool > &st, vector< int > &match)
Definition: acwing.cpp:7522
unordered_map< int, int > um
Definition: leetcode.h:3287

引用了 acwing::acwing861::find(), keys , 以及 um.

◆ put()

void leetcode::lru_cache::LRUCache::put ( int  key,
int  value 
)

如果关键字 key 已经存在,则变更其数据值 value ;如果不存在,则向缓存中插入该组 key-value 。如果插入操作导致关键字数量超过 capacity ,则应该 逐出 最久未使用的关键字。

在文件 leetcode.cpp9386 行定义.

9386 {
9387 if(um.size() == capacity) {
9388 if(um.contains(key)) {
9389 //满 含
9390 if(key != keys.back()) {
9391 keys.erase(find(keys.begin(), keys.end(), key));
9392 keys.emplace_back(key);
9393 }
9394 } else {
9395 //满 不含
9396 um.erase(*keys.begin());
9397 keys.erase(keys.begin());
9398 keys.emplace_back(key);
9399 }
9400 } else {
9401 if(um.contains(key)) {
9402 //未满 含
9403 if(key != keys.back()) {
9404 keys.erase(find(keys.begin(), keys.end(), key));
9405 keys.emplace_back(key);
9406 }
9407 } else {
9408 //未满 不含
9409 keys.emplace_back(key);
9410 }
9411 }
9412 um[key] = value;
9413 }

引用了 capacity, acwing::acwing861::find(), keys , 以及 um.

类成员变量说明

◆ capacity

int leetcode::lru_cache::LRUCache::capacity
private

在文件 leetcode.h3285 行定义.

被这些函数引用 put().

◆ keys

list<int> leetcode::lru_cache::LRUCache::keys
private

在文件 leetcode.h3286 行定义.

被这些函数引用 get() , 以及 put().

◆ um

unordered_map<int, int> leetcode::lru_cache::LRUCache::um
private

在文件 leetcode.h3287 行定义.

被这些函数引用 get() , 以及 put().


该类的文档由以下文件生成: