剑指 Offer II 031. 最近最少使用缓存(中等 哈希表 链表 双向链表 设计)

剑指 Offer II 031. 最近最少使用缓存

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

实现 LRUCache 类:

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

示例:

输入
[“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 <= 10000
0 <= value <= 105
最多调用 2 * 105 次 get 和 put

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/OrIXps
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

分析

方法一:
该方法使用list进行了增删操作,时间复杂度是O(n)。学习一下list的移除操作,remove()括号里面如果是个数字那么该数字表示的是索引,如果要移除数值元素,可以remove(new Integer(key)),其中key是要删除的元素。
方法二:
该方法所有操作的时间复杂度都是O(1)。
1、定义一个双向链表,链表的节点有两个参数key和value,对应get和put方法中的参数;
2、创建头节点和尾节点,参数都是(-1, -1),(可以任意定),先令头节点和尾节点形成环;创建哈希表,存储“key”和“存储key的节点”,方便以O(1)的时间复杂度寻找存储key的节点;
3、创建删除节点的方法、添加节点到头部(或尾部)的方法、插入节点到头部(或尾部)的方法方便使用。
4、编程get和put方法即可。

题解(Java)

方法一:

class LRUCache {
    private Map<Integer, Integer> map;
    private List<Integer> list;
    private int capacity;

    public LRUCache(int capacity) {
        map = new HashMap<>(capacity);
        list = new ArrayList<>(capacity);
        this.capacity = capacity;
    }
    
    public int get(int key) {
        if (map.containsKey(key)) {
            list.remove(new Integer(key));
            list.add(key);
            return map.get(key);
        }
        return -1;
    }
    
    public void put(int key, int value) {
        if (map.containsKey(key)) {
            map.put(key, value);
            list.remove(new Integer(key));
            list.add(key);
        } else {
            if (list.size() == capacity) {
                map.remove(list.get(0));
                list.remove(new Integer(list.get(0)));
                list.add(key);
            } else {
                list.add(key);
            }
            map.put(key, value);
        }

    }
}

/**
 * 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);
 */

方法二:

class ListNode{
    public int key;
    public int value;
    public ListNode next;
    public ListNode prev;

    public ListNode(int key, int value) {
        this.key = key;
        this.value = value;
    }
}
class LRUCache {
    private ListNode head;
    private ListNode tail;
    private Map<Integer, ListNode> map;
    private int capacity;

    public LRUCache(int capacity) {
        map = new HashMap<>(capacity);
        head = new ListNode(-1, -1);
        tail = new ListNode(-1, -1);
        head.next = tail;
        tail.prev = head;
        this.capacity = capacity;
    }
    
    public int get(int key) {
        if (map.containsKey(key)) {
            ListNode node = map.get(key);
            moveToHead(node, node.value);
            return node.value;
        }
        return -1;
    }
    
    public void put(int key, int value) {
        if (map.containsKey(key)) {
            moveToHead(map.get(key), value);
        } else {
            if (map.size() == capacity) {
                ListNode toBeDeleted = tail.prev;
                deleteNode(toBeDeleted);
                map.remove(toBeDeleted.key);
            }
            ListNode node = new ListNode(key, value);
            insertToHead(node);
            map.put(key, node);
        }
    }

    private void deleteNode(ListNode node) {
        node.prev.next = node.next;
        node.next.prev = node.prev;
    }

    private void insertToHead(ListNode node) {
        node.next = head.next;
        head.next.prev = node;
        head.next = node;
        node.prev = head;
    }

    private void moveToHead(ListNode node, int value) {
        deleteNode(node);
        node.value = value;
        insertToHead(node);
    }
}

/**
 * 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);
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值