当前位置: 首页 > article >正文

手撕LRU缓存Java版(带输入输出)

由于面试手撕lru没撕出来,导致心态炸裂,今天特地练习了lru输入输出

手撕版,在每个函数里手动加上输出

public class LC146 {
    static class LRUCache{
        class Node{
            int key, value;
            Node prev, next;
            Node(int key, int value){
                this.key = key;
                this.value = value;
            }
        }
        private Node dummy = new Node(0,0);
        private int capacity;

        private Map<Integer, Node> map = new HashMap<Integer, Node>();
        public LRUCache(int capacity){
            this.capacity = capacity;
            dummy.prev = dummy;
            dummy.next = dummy;
            System.out.print("null,");
        }

        public int get(int key) {
            Node node = getNode(key);
            if(node == null){
                System.out.print("-1,");
                return -1;
            }else{
                System.out.print(node.value+",");
                return node.value;
            }
        }

        private Node getNode(int key) {
            Node node = map.getOrDefault(key, null);
            if(node == null){
                return null;
            }
            deleteNode(node);
            pushFront(node);
            return node;
        }

        private void deleteNode(Node node) {
            node.next.prev = node.prev;
            node.prev.next = node.next;
        }

        private void pushFront(Node node) {
            node.next = dummy.next;
            node.prev = dummy;
            node.prev.next = node;
            node.next.prev = node;
        }

        public void put(int key, int value) {
            Node node = getNode(key);
            if(node != null){
                node.value = value;
                return ;
            }
            node = new Node(key, value);
            pushFront(node);
            map.put(key, node);
            if(map.size() > capacity){
                map.remove(dummy.prev.key);
                deleteNode(dummy.prev);
            }
            System.out.print("null,");
        }
    }

    public static void main(String[] args) {
        LRUCache lRUCache = new LRUCache(2);
        lRUCache.put(1, 1); // 缓存是 {1=1}
        lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
        lRUCache.get(1);    // 返回 1
        lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
        lRUCache.get(2);    // 返回 -1 (未找到)
        lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
        lRUCache.get(1);    // 返回 -1 (未找到)
        lRUCache.get(3);    // 返回 3
        lRUCache.get(4);    // 返回 4
    }
}

leetcode输入输出格式版

public class LC146 {
    static class LRUCache{
        class Node{
            int key, value;
            Node prev, next;
            Node(int key, int value){
                this.key = key;
                this.value = value;
            }
        }
        private Node dummy = new Node(0,0);
        private int capacity;

        private Map<Integer, Node> map = new HashMap<Integer, Node>();
        public LRUCache(int capacity){
            this.capacity = capacity;
            dummy.prev = dummy;
            dummy.next = dummy;
            System.out.print("null,");
        }

        public int get(int key) {
            Node node = getNode(key);
            if(node == null){
                System.out.print("-1,");
                return -1;
            }else{
                System.out.print(node.value+",");
                return node.value;
            }
        }

        private Node getNode(int key) {
            Node node = map.getOrDefault(key, null);
            if(node == null){
                return null;
            }
            deleteNode(node);
            pushFront(node);
            return node;
        }

        private void deleteNode(Node node) {
            node.next.prev = node.prev;
            node.prev.next = node.next;
        }

        private void pushFront(Node node) {
            node.next = dummy.next;
            node.prev = dummy;
            node.prev.next = node;
            node.next.prev = node;
        }

        public void put(int key, int value) {
            Node node = getNode(key);
            if(node != null){
                node.value = value;
                return ;
            }
            node = new Node(key, value);
            pushFront(node);
            map.put(key, node);
            if(map.size() > capacity){
                map.remove(dummy.prev.key);
                deleteNode(dummy.prev);
            }
            System.out.print("null,");
        }
    }

    public static void main(String[] args) {
        String[] s = new String[]{"LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"};
        int[][] inputs = new int[][]{{2}, {1, 1}, {2, 2}, {1}, {3, 3}, {2}, {4, 4}, {1}, {3}, {4}};
        int n = s.length;
        LRUCache lruCache = null;
        for(int i=0; i<n; i++){
            if(s[i].equals("LRUCache")){
                lruCache = new LRUCache(inputs[i][0]);
                System.out.print("null");
            }else if(s[i].equals("put")){
                lruCache.put(inputs[i][0], inputs[i][1]);
                System.out.print("null");
            }else if(s[i].equals("get")){
                int ans = lruCache.get(inputs[i][0]);
                System.out.print(ans);
            }
            if(i != n-1){
                System.out.print(",");
            }
        }
    }
}


测试结果
在这里插入图片描述

原文地址:https://blog.csdn.net/qq_46118704/article/details/146572494
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.kler.cn/a/611607.html

相关文章:

  • 《基于机器学习发电数据电量预测》开题报告
  • 学习本地部署DeepSeek的过程(基于LM Studio)
  • 自然语言处理|金融舆情解析:智能事件抽取与风险预警之道
  • 算法-动态规划三
  • 学习threejs,使用Sprite精灵、SpriteMaterial精灵材质
  • 企业内训|DeepSeek技术革命、算力范式重构与场景落地洞察-某头部券商
  • 精选10个好用的WordPress免费主题
  • ELK stack基础架构
  • Android TextView实现跑马灯效果性能优化
  • 用shell脚本,批量备份MySQL中所有数据库,并批量还原!
  • asp.net进销存软件WEB进销存ERP软件库存玻璃行业
  • SQLite优化实践
  • 【设计模式】策略模式(Strategy Pattern)详解
  • 5分钟快速上手Docker容器化部署:从零到实践
  • 带你刷题—公因子的数目(leetcode2427)
  • docker-操作实战
  • Visual Studio 使用 IntelliCode AI 辅助代码开发
  • 【CUDA】mnist_cuda
  • 模块学习篇#2:解析常用于YOLO等深度学习模型的注意力机制CBAM
  • Oracle常用分析诊断工具(9)——ADDM