Headline tear code question: 16.25. LRU cache

Headline tear code question: 16.25. LRU cache

Many people interview headline internship, require handwriting to implement LRU cache algorithm.

Nothing to do, so write this solution

LinkedList+HashMap解法

  • Maintain a queue (LinkedList implements the queue interface), which is used to implement the least recently used strategy. The head of the queue holds the least recently used key , and the tail of the queue holds the most recently accessed key .

  • Maintain a HashMap to store <key, vaue> key-value pairs.

  • When the get method is called, the queue is updated, and the key to be accessed is moved to the end of the queue.

  • When calling the put method, if the same key exists, the hashmap is replaced, and the queue is updated to move the accessed key to the tail of the queue. If the same key does not exist, the hashmap is added and the key is added to the queue Tail. And if the HashMap size exceeds the threshold, the queue needs to be dequeued, and the HashMap needs to delete the key value pair corresponding to the dequeue key.

java code

import java.util.*;
class LRUCache {
    int capacity;
    LinkedList<Integer> queue = new LinkedList<>();
    HashMap<Integer,Integer> map = new HashMap<>();

    public LRUCache(int capacity) {
        this.capacity = capacity;
    }
    
    public int get(int key) {
        if(!map.containsKey(key)) return -1;
        queue.remove(new Integer(key)); queue.offer(key);
        return map.get(key);
    }
    //put也算访问
    public void put(int key, int value) {
        if(map.containsKey(key)) {
            queue.remove(new Integer(key));
        }else{
            if(queue.size() == capacity) {
                map.remove(queue.poll());
            }
        }
        queue.offer(key);
        map.put(key,value);
    }
}

Double linked list + HashMap solution

LinkedList + HashMap solution, there is a disadvantage, need to move the key to the end of the team THE ( n ) O (n) time complexity, because it is necessary to traverse the linked list. Use the double linked list to realize the queue function, move the key to the end of the queue, and divide it into two steps: delete the element in one step and add the end of the queue in one step. Total complexity THE ( 1 ) O (1)

Java code

import java.util.*;
class Node{
    int key;
    int value;
    Node next = null;
    Node prev = null;
    public Node(int key, int value){this.key = key; this.value = value;}
}

class LRUCache {
    int capacity;
    Node head = null;
    Node tail = null;
    HashMap<Integer, Node> map = new HashMap<>();

    public LRUCache(int capacity) {
        this.capacity = capacity;
    }
    //删除队头
    public void deleteHead(){ 
        if(tail == head) tail = tail.prev;
        Node temp = head;
        head = temp.next;
        if(head != null) head.prev = null; 
        temp.next = null;
    }
    //删除指定Node,有可能改变head、tail指向
    public void deleteNode(Node node) {
        if(node == head) head = head.next;
        if(node == tail) tail = tail.prev;
        if(node.prev != null)
            node.prev.next = node.next;
        if(node.next != null)
            node.next.prev = node.prev;
        node.next = null; node.prev = null;
    }
    //添加指定Node至队尾
    public void addLast(Node node) {
        if(head == null && tail == null){
            head = node;
            tail = node;
            return;
        }
        tail.next = node;
        node.prev = tail;
        node.next = null;
        tail = node;
    }
    public int get(int key) {
        if(!map.containsKey(key)) return -1;
        Node node = map.get(key);
        deleteNode(node); addLast(node);
        return map.get(key).value;
    }
    //put也算访问
    public void put(int key, int value) {
        Node node = null;
        if(map.containsKey(key)) {
            node = map.get(key);
            deleteNode(node);  
            node.value = value;
        }else{
            if(map.size() == capacity) {
                //删除队头
                map.remove(head.key);
                deleteHead();
            }
            node = new Node(key,value);
            map.put(key,node);
        }
        addLast(node);
    }
}
Published 187 original articles · Like 288 · Visits 140,000+

Guess you like

Origin blog.csdn.net/zycxnanwang/article/details/105445987