力扣(LeetCode)146

发布时间:2019-06-27 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了力扣(LeetCode)146脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

题目地址:
https://leetcode-cn.com/probl...
题目描述:
运用你所掌握的数据结构,设计和实现一个 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

解答:
这一题就是实现一个最近最少使用的缓存。我们根据操作系统的定义可以想出一个比较朴素的方法:
利用队列(基于链表的队列),每放入一个数据就入队列(尾部插入),而淘汰一个数据就出队列(从头部淘汰)。
而更新一个数据就把这个数据先删除,然后再入队列。这样就能够保证最近最少使用。
这种做法,如果永远不更新数据,每次插入的数据都一定是不在队列里的,那么能够保证put()是O(1)。
但是无法保证get()是O(1),也无法保证更新数据时是O(1),因为这两个操作必然要遍历队列。

如何保证get()是O(1)复杂度呢?利用HashSet,可以用常数时间查询是否有这个元素?而如何使得更新也是O(1)呢?
如果我们的链表是双向链表,并且维护表头,表尾,再利用一个HashMap定位到这个链表节点(key是这个节点的key,value是这个节点的引用),就可以使得更新也是O(1)。
不过这里可以把HashSet和HashMap合起来用,只用HashMap即可。因为可以通过HashMap.get(key) == null?来判断是否有这个节点。

java ac代码:

class LRUCache {
    //使用HashMap+双向链表才能达到O(1)
    HashMap<Integer,Node>map;
    int capacity;
    int size;
    Node head,tail;

    public LRUCache(int capacity) {
        this.capacity = capacity;
        map = new HashMap(capacity*2);
        head = new Node(0,0);
        tail = new Node(0,0);
        head.next = tail;
        tail.pre = head;
        
    }
    
    public int get(int key) {
        Node node = map.get(key);
        if(node != null)
        {
            
            if(node.next != tail)
            {
                node.pre.next = node.next;
                node.next.pre = node.pre;
                
                node.next = tail;
                node.pre = tail.pre;
                tail.pre.next = node;
                tail.pre = node;
                
            }
            return node.value;
        }
        
        return -1;
        
    }
    
    public void put(int key, int value) {
        if(get(key) != -1)
        {
            tail.pre.value = value;
            return;
        }
        if(size < capacity)
            size++;
        else
        {
            Node node = head.next;
            map.remove(node.key);
            head.next = node.next;
            node.next.pre = head;
        }
        Node node = new Node(key,value);
        map.put(key,node);
        node.next = tail;
        node.pre = tail.pre;
        tail.pre.next = node;
        tail.pre = node;
    }
    
    class Node
    {
        int key,value;
        Node pre,next;
        public Node(int key,int value)
        {
            this.key = key;
            this.value = 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);
 */


脚本宝典总结

以上是脚本宝典为你收集整理的力扣(LeetCode)146全部内容,希望文章能够帮你解决力扣(LeetCode)146所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: