LRU缓存机制(java)

题目介绍:

运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制 。

实现 LRUCache 类:

LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存
int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
void put(int key, int value) 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上
限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。

进阶:你是否可以在 O(1) 时间复杂度内完成这两种操作?

示例:

输入
[“LRUCache”, “put”, “put”, “get”, “put”, “get”, “put”, “get”, “get”, “get”]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
输出
[null, null, null, 1, null, -1, null, -1, 3, 4]

解释
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // 缓存是 {1=1}
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
lRUCache.get(1); // 返回 1
lRUCache.put(3, 3); // 该操作会使得关键字 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

提示:

1 <= capacity <= 3000
0 <= key <= 3000
0 <= value <= 104
最多调用 3 * 104 次 get 和 put

解题思路,要实现O(1)的时间复杂度,我们首先相当的就是利用hashmap来存储,本题的难点在于如何在达到容量后删除最久没有使用过的数据,这里我们使用双链表来记录数据被使用的时间关系,将最近使用的数据放于链表头部,依次指向其他节点,每次查询或者更改某一数据,就将其置于表头,如果在插入数据时,已经达到容量,就将表尾的数据删除。

具体代码如下:

class LRUCache {
    private int  capacity;
    private Map<Integer,LRUNode> map=new HashMap<>();

    private LRUNode firstNode=new LRUNode(-1,-1);
    private LRUNode lastNode=new LRUNode(-1,-1);
    //当前数据长度
    private int size=0;
    public LRUCache(int capacity) {
        this.capacity=capacity;
    }

    public int get(int key) {
        LRUNode lruNode=map.get(key);
        if (map.get(key)==null){
            return -1;
        }
        MoveToHead(lruNode);
        return lruNode.value;
    }

    private void MoveToHead(LRUNode lruNode) {
        lruNode.pre.next=lruNode.next;
        lruNode.next.pre=lruNode.pre;
        lruNode.next=firstNode.next;
        firstNode.next.pre=lruNode;
        lruNode.pre=firstNode;
        firstNode.next=lruNode;
    }

    public void put(int key, int value) {
        if (map.get(key)==null) {
            if (map.size() == 0) { //插入第一个数的时候,将第一个和最后一个都
                LRUNode lruNode = new LRUNode(key, value);
                map.put(key, lruNode);
                firstNode.next = lruNode;
                lruNode.pre = firstNode;
                lastNode.pre = lruNode;
                lruNode.next = lastNode;
                size++;
            } else if (map.size() == capacity) {
                LRUNode lruNode = new LRUNode(key, value);
                map.put(key,lruNode);

                lruNode.next = firstNode.next;
                firstNode.next.pre = lruNode;
                lruNode.pre = firstNode;
                firstNode.next = lruNode;
                //记录最后一个节点的key
                int lastkey=lastNode.pre.key;
                lastNode.pre = lastNode.pre.pre;
                lastNode.pre.next = lastNode;
                map.remove(lastkey);
            } else {
                LRUNode lruNode = new LRUNode(key, value);
                map.put(key, lruNode);
                lruNode.next = firstNode.next;
                firstNode.next.pre = lruNode;
                firstNode.next = lruNode;
                lruNode.pre = firstNode;
                size++;
            }
        }else {
            LRUNode lruNode=map.get(key);
            lruNode.value=value;
            MoveToHead(lruNode);
        }
    }
    private class LRUNode{
        Integer key;
        Integer value;
        LRUNode(Integer key,Integer value){
            this.key=key;
            this.value=value;
        }
        LRUNode pre;  //每个节点指向的前一个节点
        LRUNode next; //每个节点指向的后一个节点
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值