题解 | #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;
        }
    }

}


全部评论

相关推荐

不愿透露姓名的神秘牛友
06-27 14:11
很喜欢小米的新车,校招薪资每月22k,攒多久能买?
测试糕手手:别看工资,先看现金流存款。有50W存款以上再考虑,车是消耗品,选适合自己的重要。你有钱就当我没说过
点赞 评论 收藏
分享
点赞 评论 收藏
分享
05-14 20:34
门头沟学院 Java
窝补药贝八股:管他们,乱说,反正又不去,直接说680
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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