力扣(LeetCode)146

栏目: 数据库 · 发布时间: 6年前

内容简介:题目地址:题目描述:

题目地址:

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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Effective Java

Effective Java

Joshua Bloch / Addison-Wesley Professional / 2018-1-6 / USD 54.99

The Definitive Guide to Java Platform Best Practices—Updated for Java 9 Java has changed dramatically since the previous edition of Effective Java was published shortly after the release of Jav......一起来看看 《Effective Java》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具