萤火小屋

优律的知识库

  • 首页
  • 归档
  • 分类
  • 标签
  • 留言
  • 关于

  • 搜索
消息队列 RabbitMQ Redis 双指针 力扣 动态代理 Git YAML SpringBoot SpringMVC 回溯算法 分治算法 归并排序 快排 手撕 事务 MySQL索引 MySQL 小技巧 Spring Framework Spring 动态规划 Linux Android 贪心算法 操作系统 进程调度模拟 IPv6 数据库 计算机组成原理 计算机基础 栈 Java 静态路由 路由器 交换机 数字通信 网络工程 计算机网络 Web http 大学学习技巧 程序设计 算法

力扣第146题-LRU缓存

发表于 2023-07-03 | 分类于 力扣刷题 | 0 | 阅读次数 13

题目
链接

题解

import java.util.HashMap;
import java.util.Map;

class LRUCache {

    // 缓存哈希表
    private Map<Integer, Node> cache;
    // 最大容量
    private int max;
    // 当前容量
    private int cap;
    // 缓存头指针
    private Node head;
    // 缓存尾指针
    private Node tail;

    // 缓存节点
    private static class Node {
        public int key;
        public int value;
        public Node next;
        public Node prev;
        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
        public Node(int key, int value, Node prev, Node next) {
            this.key = key;
            this.value = value;
            this.prev = prev;
            this.next = next;
        }
        public Node() {}
    }

    public LRUCache(int capacity) {
        cache = new HashMap<>(capacity);
        this.max = capacity;
        this.cap = 0;
        head = new Node();
        tail = head;
    }
    
    public int get(int key) {
        Node node = cache.get(key);
        if (node == null) {
            return -1;
        }
        // 用到一次移动到头指针next
        trafficToHeadNext(node);
        return node.value;
    }
    
    public void put(int key, int value) {
        Node node = cache.get(key);
        // 覆盖
        if (node != null) {
            node.value = value;
            // 用到一次移动到头指针next
            trafficToHeadNext(node);
            return;
        }
        // 新增
        else {
            // 新增节点插入头节点next
            node = new Node(key, value, head, head.next);
            cache.put(key, node);
            head.next = node;
            if (node.next != null) {
                node.next.prev = node;
            }
            if (tail == head) {
                tail = node;
            }
            // 容量溢出
            if (cap + 1 > max) {
                // 移除尾节点
                tail.prev.next = null;
                cache.remove(tail.key);
                tail = tail.prev;
            } else {
                cap++;
            }
        }
    }

    /**
     * 将节点移动到head的next处
     */
    private void trafficToHeadNext(Node node) {
        if (head.next == node) {
            return;
        }
        // 尾指针变更
        if (node == tail) {
            tail = node.prev;
        }
        // 摘除当前节点
        Node prev = node.prev;
        Node next = node.next;
        prev.next = next;
        if (next != null) {
            next.prev = prev;
        }
        // 当前节点移动到头节点next
        node.next = head.next;
        if (node.next != null) {
            node.next.prev = node;
        }
        head.next = node;
    }

}

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache obj = new LRUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.put(key,value);
 */
# 算法 # 程序设计 # 力扣
力扣第141题-环形链表
力扣第169题-多数元素
  • 文章目录
  • 站点概览
优律

优律

优律的知识库

125 日志
20 分类
44 标签
E-mail Twitter Instagram
Links
  • CZLisyx - 浮生志
  • Vedfolnir
0%
© 2019 — 2023 萤火小屋——优律的博客网站
网站已勉强运行 
Halo博客系统技术支持