【东华大学oj】LRU缓存

LRU缓存

作者: Turbo

时间限制: 1s

章节: 课程设计

问题描述

请你设计并实现一个满足  LRU (最近最少使用) 缓存 约束的数据结构。

实现 LRUCache 类:

    LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存

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

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

函数 get 和 put 最好以 O(1) 的平均时间复杂度运行。

示例:

输入

2

put 1 1

put 2 2

get 1

put 3 3

get 2

put 4 4

get 1

get 3

get 4

输出

1

-1

-1

3

4

解释

LRUCache lRUCache = new LRUCache(2);//容量为2,即最多只能保存两个关键字

lRUCache.put(1, 1); // 缓存是 {1=1}

lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}

lRUCache.get(1);    // 返回 1。注意,这里使用了1

lRUCache.put(3, 3); // 该操作会使得关键字 2 作废(在关键字1和2中,2是最久未被使用的),缓存是 {1=1, 3=3}

lRUCache.get(2);    // 返回 -1 (未找到)

lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}

lRUCache.get(1);    // 返回 -1 (未找到)

lRUCache.get(3);    // 返回 3

lRUCache.get(4);    // 返回 4

输入说明

输入若干行:

第一行输入一个整数capacity表示LRU缓存的容量。

后面每行输入为put或get:

    如果指令为put类型,后面需要输入两个整数表示key和value。

    如果指令为get类型,后面需要输入一个整数表示key。

提示:

    1 <= capacity <= 3000

    0 <= key <= 10000

    0 <= value <= 10^5

    最多调用 2 * 10^5 次 get 和 put

输出说明

输出若干行:

每行一个整数,表示为get指令的返回值。

#include <iostream>
#include <unordered_map>
#include <list>
using namespace std;

class LRUCache {
private:
    int capacity;
    list<pair<int, int>> cache; // 使用双向链表来维护元素的顺序
    unordered_map<int, list<pair<int, int>>::iterator> hashmap; // 使用哈希表来快速查找元素

public:
    LRUCache(int capacity) : capacity(capacity) {}

    int get(int key) {
        auto it = hashmap.find(key);
        if (it == hashmap.end()) {
            return -1; // 没有找到元素,返回-1
        }
        // 将找到的元素移到链表的头部
        cache.splice(cache.begin(), cache, it->second);
        return it->second->second;
    }

    void put(int key, int value) {
        auto it = hashmap.find(key);
        if (it != hashmap.end()) {
            // 元素已经存在,更新值并移到链表头部
            it->second->second = value;
            cache.splice(cache.begin(), cache, it->second);
        } else {
            // 元素不存在,需要插入新元素
            if (cache.size() == capacity) {
                // 缓存已满,移除链表尾部的元素
                hashmap.erase(cache.back().first);
                cache.pop_back();
            }
            // 插入新元素到链表头部
            cache.emplace_front(key, value);
            hashmap[key] = cache.begin();
        }
    }
};

int main() {
    int capacity;
    cin >> capacity;
    LRUCache lruCache(capacity);

    string command;
    while (cin >> command) {
        if (command == "put") {
            int key, value;
            cin >> key >> value;
            lruCache.put(key, value);
        } else if (command == "get") {
            int key;
            cin >> key;
            cout << lruCache.get(key) << endl;
        }
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Juneeeeeeeeeeeee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值