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

}


全部评论

相关推荐

双飞二本嵌入式求拷打我是在&nbsp;BOSS&nbsp;上投递的简历,好多都没人回复,这是开场白和简历求大神帮忙看看。您好!我是2025届应届生,最快可在一周内上岗,能够实习六个月以上,并接受加班。以下是我的核心优势和相关经验:1.&nbsp;嵌入式开发能力:&nbsp;&nbsp;&nbsp;熟练掌握STM32系列单片机及其外设(如GPIO、定时器、ADC、DAC、I2C、SPI、UART等),能够独立完成硬件驱动开发和调试。&nbsp;&nbsp;熟悉FreeRTOS实时操作系统,具备多任务调度和资源管理经验。&nbsp;&nbsp;熟悉LVGL图形库开发,能够实现嵌入式设备的图形界面设计。2.&nbsp;硬件设计能力:&nbsp;&nbsp;&nbsp;具备PCB设计经验,曾为2023年工创赛物流搬运赛道设计小车主板,带领团队获得国家级银奖。&nbsp;&nbsp;&nbsp;熟悉硬件原理图分析,能够快速理解并调试硬件电路。3.&nbsp;机器人开发与竞赛经验:&nbsp;&nbsp;&nbsp;在全国大学生智能车竞赛、ROS机器人竞赛中多次获得国家级奖项,具备丰富的机器人开发经验。&nbsp;&nbsp;&nbsp;熟悉Linux环境,对ROS和ROS&nbsp;2有一定了解,能够进行机器人系统的开发与调试。4.&nbsp;编程能力:&nbsp;&nbsp;&nbsp;熟悉C/C++,熟悉Python,能够高效完成嵌入式开发和算法实现。&nbsp;&nbsp;&nbsp;具备良好的代码规范和文档编写能力。5.&nbsp;团队协作与领导能力:&nbsp;&nbsp;&nbsp;在多个项目中担任核心开发或团队负责人,具备良好的沟通能力和团队协作精神。&nbsp;&nbsp;&nbsp;在工创赛中带领团队完成项目规划、任务分配和技术攻关,展现了较强的领导力。我对嵌入式开发、机器人技术和智能硬件充满热情,期待加入贵公司,与团队共同成长,为公司创造价值!如果有合适的岗位,欢迎随时联系我,期待进一步沟通!
沉淀一会:嵌入式就是狗屎
点赞 评论 收藏
分享
数开小菜鸡:你是我今早见过的最美的牛客女孩......
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务