Golden programmer interview - face questions 16.25 LRU cache

topic:

Design and build a "least recently used" cache that will delete the least recently used items. It should be mapped to the cache from the key value (key allows you to insert and retrieve the corresponding value of the particular), and specify the maximum capacity at initialization. When the cache is filled, it should delete the least recently used items.

It should support the following: Obtain the data  get and write data  put .

Data acquisition  get(key) - if the key (key) is present in the cache, then the value of the key is acquired (always positive), otherwise -1.
Writing data  put(key, value) - if the key is not present, then the data value is written. When the cache capacity limit is reached, it should delete the least recently used data values before writing new data to make room for new data value.

Example:

LRUCache cache = new LRUCache (2 / * buffer size * /); 

cache.put (. 1,. 1); 
cache.put (2, 2); 
cache.get (. 1); // returns. 1 
cache.put (. 3, 3); // this operation will make the key 2 void 
cache.get (2); // returns -1 (not found) 
cache.put (. 4,. 4); // this operation will cause an invalid key 
cache. get (1); // returns -1 (not found) 
cache.get (. 3); // returns. 3 
cache.get (4); // returns 4

analysis:

LeetCode 146. LRU CacheLRU caching mechanism (C ++ / Java) refer to this explanation.

program:

class LRUCache {

    LRUCache(int capacity) {
        this.capacity = capacity;
        this.map = new HashMap<>();
        this.dummyHead = new Node(0, 0);
        this.tail = new Node(0, 0);

        dummyHead.prev = null;
        dummyHead.next = tail;
        tail.prev = dummyHead;
        tail.next = null;
    }
    
    int get(int key) {
        if(map.containsKey(key)) {
            Node node = map.get(key);
            int value = node.val;
            removeNode(node);
            addToHead(node);
            return value;
        }
        return -1;
    }
    
    void put(int key, int value) {
        if(map.containsKey(key)){
            Node node = map.get(key);
            removeNode(node);
            map.remove(key);
            size--;
        }
        Node node = new Node(key, value);
        map.put(key, node);
        if(size < capacity) {
            addToHead(node);
            size++;
        }else{
            map.remove(tail.prev.key);
            removeNode(tail.prev);
            addToHead(node);
        }
    }
    private void removeNode (Node node){
        node.prev.next = node.next;
        node.next.prev = node.prev;
    }
    private void addToHead(Node node){
        node.next = dummyHead.next;
        node.next.prev = node;
        dummyHead.next = node;
        node.prev = dummyHead;
    }
    class Node{
        int key;
        int val;
        Node prev;
        Node next;
        public Node(int key, int val){
            this.key = key;
            this.val = val;
        }
    }
    private HashMap<Integer, Node> map;
    private int capacity;
    private Node dummyHead;
    private Node tail;
    private int size;
}

/**
 * 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 LRUCache {

    LRUCache(int capacity) {
        this.capacity = capacity;
        map = new LinkedHashMap<>();
    }
    
    int get(int key) {
        if(map.containsKey(key)) {
            int value = map.get(key);
            map.remove(key);
            map.put(key, value);
            return value;
        }
        return -1;
    }
    
    void put(int key, int value) {
        if(map.containsKey(key)) {
            map.remove(key);
        }
        map.put(key, value);
        if(map.size() > capacity) {
            map.remove(map.keySet().iterator().next());
        }
    }
    private int capacity;
    private LinkedHashMap<Integer, Integer> map;
}

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

 

Guess you like

Origin www.cnblogs.com/silentteller/p/12531707.html