力扣:146. LRU 缓存机制

题目转载自:力扣icon-default.png?t=L892https://leetcode-cn.com/problems/lru-cache/

 

解答:


public class LRUCache {

    class CacheNode {
        Integer key;
        Integer value;
        CacheNode prePointer;
        CacheNode nextPointer;

        public CacheNode(Integer key, Integer value, CacheNode prePointer, CacheNode nextPointer) {
            this.key = key;
            this.value = value;
            this.prePointer = prePointer;
            this.nextPointer = nextPointer;
        }
    }

    private int capacity;

    private Map<Integer, CacheNode> cache;

    private CacheNode head;

    private CacheNode tail;

    public LRUCache(int capacity) {
        this.capacity = capacity;
        this.cache = new HashMap<>(capacity);
        this.head = new CacheNode(Integer.MIN_VALUE, Integer.MIN_VALUE, null, null);
        this.tail = new CacheNode(Integer.MAX_VALUE, Integer.MAX_VALUE, head, null);
        head.nextPointer = tail;
    }

    public int get(int key) {
        CacheNode cacheNode = cache.get(key);
        if (cacheNode == null) {
            return -1;
        }
        if (cacheNode.prePointer == head) {
            return cacheNode.value;
        }
        // 先把这个结点拆下来
        cacheNode.prePointer.nextPointer = cacheNode.nextPointer;
        cacheNode.nextPointer.prePointer = cacheNode.prePointer;
        // 把这个结点放到头部
        CacheNode tempNode = head.nextPointer;
        head.nextPointer = cacheNode;
        cacheNode.prePointer = head;
        tempNode.prePointer = cacheNode;
        cacheNode.nextPointer = tempNode;
        return cacheNode.value;
    }

    public void put(int key, int value) {
        CacheNode cacheNode = cache.get(key);
        if (cacheNode == null) {
            // put
            if (cache.size() == capacity) {
                // 删除尾部的结点
                CacheNode lastNode = tail.prePointer;
                CacheNode lastPreNode = lastNode.prePointer;
                lastPreNode.nextPointer = tail;
                tail.prePointer = lastPreNode;
                cache.remove(lastNode.key);
                lastNode.nextPointer = null;
                lastNode.prePointer = null;
                lastNode = null;
            }
            // insert
            CacheNode newNode = new CacheNode(key, value, null, null);
            CacheNode tempNode = head.nextPointer;
            head.nextPointer = newNode;
            newNode.prePointer = head;
            tempNode.prePointer = newNode;
            newNode.nextPointer = tempNode;
            cache.put(key, newNode);
            return;
        }
        // update
        // 删除原来的结点
        CacheNode cacheNodePrePointer = cacheNode.prePointer;
        cacheNodePrePointer.nextPointer = cacheNode.nextPointer;
        cacheNode.nextPointer.prePointer = cacheNode.prePointer;
        // 移到头部
        cacheNode.value = value;
        CacheNode tempNode = head.nextPointer;
        head.nextPointer = cacheNode;
        cacheNode.prePointer = head;
        tempNode.prePointer = cacheNode;
        cacheNode.nextPointer = tempNode;
    }

    public static void main(String[] args) {
        LRUCache lruCache = new LRUCache(2);
        lruCache.put(2, 1);
        lruCache.put(1, 1);
        lruCache.put(2, 3);
        lruCache.put(4, 1);
        int i = lruCache.get(1);
        System.out.println(i);
        i = lruCache.get(2);
        System.out.println(i);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值