LRU算法

LRU

1、用Java自带的LinkedHashMap实现
public class LRUCache {
    int capacity;
    Map<Integer, Integer> map;

    public LRUCache(int capacity) {
        this.capacity = capacity;
        map = new LinkedHashMap<>((int)((float)capacity / 0.75f + 1.0f), 0.75f, true);
    }

    public int get(int key) {
        Integer value = map.get(key);
        if (value == null)
            return -1;
        return value;
    }


    public void put(int key, int value) {
        map.put(key, value);
        if (map.size() > capacity) {
            map.remove(map.entrySet().iterator().next().getKey());
        }
    }
}
2、用双向链表实现
public class LRUCache {
	
    // 链表节点类
    static class Node {
        Node pre;
        Node next;
        int key;
        int value;

        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }

    Map<Integer, Node> map;
    int capacity;
    Node head;
    Node tail;

    public LRUCache(int capacity) {
        this.capacity = capacity;
        this.map = new HashMap<>();
        this.head = new Node(-1, -1);
        this.tail = new Node(-1, -1);
        head.next = tail;
        tail.pre = head;
    }

    public int get(int key) {
        if (!map.containsKey(key)) return -1;
        
        Node temp = map.get(key);
        temp.pre.next = temp.next;
        temp.next.pre = temp.pre;
        moveToTail(temp);
        return temp.value;
    }


    public void put(int key, int value) {
        if (get(key) != -1) {
            map.get(key).value = value;
            return;
        }

        Node temp = new Node(key, value);
        map.put(key, temp);
        moveToTail(temp);
        if (map.size() > capacity) {
            map.remove(head.next.key);
            head.next = head.next.next;
            head.next.pre = head;
        }
    }
	
    // 移动节点到最后
    public void moveToTail(Node node) {
        node.pre = tail.pre;
        tail.pre.next = node;
        tail.pre = node;
        node.next = tail;
    }

}
3、单项链表实现
public class LRUCache {

    static class Node {
        Node next;
        int value;
        int key;

        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }

    Map<Integer, Node> map;
    int capacity;
    Node head;
    Node tail;

    public LRUCache(int capacity) {
        this.capacity = capacity;
        this.map = new HashMap<>();
        this.head = new Node(-1, -1);
        this.tail = head;
    }

    public int get(int key) {
        if (!map.containsKey(key)) return -1;
        Node pre = map.get(key);
        Node cur = pre.next;

        if (cur != tail) {
            pre.next = cur.next;
            map.put(cur.next.key, pre);
            map.put(cur.key, tail);
            moveToTail(cur);
        }
        return cur.value;
    }


    public void put(int key, int value) {
        if (get(key) != -1) {
            map.get(key).next.value = value;
            return;
        }
        Node node = new Node(key, value);
        map.put(key, tail);
        moveToTail(node);

        if(map.size() > capacity) {
            map.remove(head.next.key);
            map.put(head.next.next.key, head);
            head.next = head.next.next;
        }
    }

    public void moveToTail(Node node) {
        node.next = null;
        tail.next = node;
        tail = tail.next;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值