题解 | #LRU Cache#

LRU Cache

https://www.nowcoder.com/practice/3da4aeb1c76042f2bc70dbcb94513338

解题思路:

最近访问的元素放在第一个,其他的往后挪,可以用双向链表LinkedList来实现

需要实现两个方法:put和get

  • get的时候需要调整优先级,要把当前访问的item移到第一个
  • put需要实现更新/增加/替换的操作
  • 更新:key已经存在的情况,找到对应的item,更新value
  • 增加:key不存在,cache没有满,即cache size小于capacity,那么直接将它加到第一个
  • 替换:key不存在,cache满了,即cache size等于capacity,那么要将最后一个元素删除,然后将当前的item加入到第一个

put和get都有查找findEntryByKey操作,时间复杂度是O(n),如果要优化成O(1)的话,需要用哈希表来存储key对应的KeyValue

import java.util.Scanner;
import java.util.*;

public class Main {
    private static LinkedList<KeyValue> cache = new LinkedList<KeyValue>();
    private static Map<Integer, KeyValue> lookUp = new HashMap<Integer, KeyValue>();
    private static int capacity = 0;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNextLine()) {
            String[] input = in.nextLine().split(" ");
            if (input.length == 1) {
                capacity = Integer.valueOf(input[0]);
                continue;
            }

            if (input.length == 2) {
                handleGetCommand(input);
            }

            if (input.length == 3) {
                handlePushCommand(input);
            }
        }
    }

    private static void handleGetCommand(String[] input) {
        int key = Integer.valueOf(input[1]);
        KeyValue target = findEntryByKey(key);
        if (target == null) {
            System.out.println("-1");
            return;
        }
        int index = cache.indexOf(target);
        ajustPriority(index);
        System.out.println(target.getValue());
    }

    private static void handlePushCommand(String[] input) {
        int key = Integer.valueOf(input[1]);
        int value = Integer.valueOf(input[2]);

        KeyValue target = findEntryByKey(key);

        if (target != null) {
            int index = cache.indexOf(target);
            cache.get(index).setValue(value);
            lookUp.get(key).setValue(value);
            return;
        }

        if (capacity < 1) {
            return;
        }

        if (cache.size() == capacity) {
            KeyValue toRemove = cache.getLast();
            lookUp.remove(toRemove.key);
            cache.remove(toRemove);
        }
        KeyValue newItem = new KeyValue(key, value);
        cache.addFirst(newItem);
        lookUp.put(key, newItem);
    }

    private static KeyValue findEntryByKey(int key) {
        return lookUp.get(Integer.valueOf(key));
    }

    private static void ajustPriority(int index) {
        KeyValue target = cache.get(index);
        cache.remove(target);
        cache.addFirst(target);
    }

    private static class KeyValue {
        private int key;
        private int value;
        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public int getKey() {
            return key;
        }

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

}


全部评论

相关推荐

2025-11-22 14:21
未填写教育信息 Java
程序员花海:实习太简单了 学历可以的 实习描述应该是先介绍业务 再介绍技术 技术咋推动业务的 做到了啥收益 有没有做实验 实验组和对照组有什么不同 你最后学到了什么 有没有参与处理过线上问题 有没有参与过公司的code review 有没有参与过技术分享 这些都是可以在实习描述中写的 并且实习和项目不一样不会撞车 应该放在最前面 放在教育背景下面 另外项目有点烂大街 可以看下我主页的简历优化案例
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务