leetcode@ [146] LRU Cache (TreeMap)

Posted 流白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode@ [146] LRU Cache (TreeMap)相关的知识,希望对你有一定的参考价值。

https://leetcode.com/problems/lru-cache/

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

 

class pair {
    public int key;
    public int value;
    public pair(int k, int v) {
        super();
        this.key = k;
        this.value = v;
    }
    
    public void setValue(int value) {
        this.value = value;
    }
    
}

class cmp implements Comparator {
    
    public int compare(Object o1, Object o2) {
        pair p1 = (pair) o1;
        pair p2 = (pair) o2;
        
        if(p1.key < p2.key) {
            return -1;
        } else if(p1.key == p2.key) {
            if(p1.value == p2.value) {
                return 0;
            } else if(p1.value < p2.value) {
                return -1;
            } else {
                return 1;
            }
        } else {
            return 1;
        }
    }
}
public class LRUCache {
    
    public Set<pair> stack = null;
    public HashMap<Integer, Integer> mapping = null;
    public TreeMap<Integer, Integer> timeToKey = null;
    public TreeMap<Integer, Integer> keyToTime = null;
    public int cap = 0;
    public int counter = 0;
    
    public LRUCache(int capacity) {
        this.mapping = new HashMap<Integer, Integer> ();
        this.timeToKey = new TreeMap<Integer, Integer> ();
        this.keyToTime = new TreeMap<Integer, Integer> ();
        this.cap = capacity;
        this.counter = 0;
    }
    
    public int get(int key) {
        
        if(!mapping.containsKey(key)) {
            return -1;
        } else {
            
            counter++;
            int value = mapping.get(key);
            
            int time = keyToTime.get(key);
            keyToTime.put(key, counter);
            
            timeToKey.remove(time);
            timeToKey.put(counter, key);
            
            return value;
        }
    }
    
    public void set(int key, int value) {
        
        counter++;
        
        if(mapping.containsKey(key)) {
            
            int time = keyToTime.get(key);
            keyToTime.put(key, counter);
            
            timeToKey.remove(time);
            timeToKey.put(counter, key);
            
            mapping.put(key, value);
            
        } else {
            
            if(mapping.size() < cap) {
                
                mapping.put(key, value);
                keyToTime.put(key, counter);
                timeToKey.put(counter, key);
                
            } else {
                
                int lru = timeToKey.pollFirstEntry().getValue();
                mapping.remove(lru);
                mapping.put(key, value);
                
                keyToTime.put(key, counter);
                timeToKey.put(counter, key);
            }
        }
    }
}
View Code

 

以上是关于leetcode@ [146] LRU Cache (TreeMap)的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 146 LRU Cache

leetcode146- LRU Cache- hard

[LeetCode] 146. LRU Cache

LeetCode Top 100 Liked Questions 146. LRU Cache (Java版; Medium)

146. LRU Cache

146. LRU Cache(js)