牛客NC93 设计LRU缓存结构【hard 链表,Map Java】

题目


题目链接:

https://www.nowcoder.com/practice/5dfded165916435d9defb053c63f1e84

思路

复制代码
	双向链表+map
	最新的数据放头结点,尾节点放最老的数据,没次移除尾巴节点
	本地考察链表的新增,删除,移动节点

参考答案Java

java 复制代码
import java.util.*;


public class Solution {
    Map<Integer, Node> cache = new HashMap<>();
    Node start, end;
    int cap = 0;
    public Solution(int capacity) {
        // write code here
        cap = capacity;
    }

    public int get(int key) {
        //key对应节点移动到头部,成为头节点
        if (!cache.containsKey(key)) return -1;

        Node cur = cache.get(key);
        int v = cur.data;


        Node next = cur.next;
        Node prev = cur.prev;


        if (next != null && prev != null) { //cur 要变成头结点
            next.prev = prev;
            prev.next = next;

            if (next.next == null) { //这里似乎可以不要
                end = next;
            }

            cur.next = start;
            start.prev = cur;
            start = cur;
        } else if (next != null) { //说明cur是头结点,不管了

        } else if (prev != null) { //自己是尾结点
            prev.next = null; //自己的prev要成为尾巴,prev.next设置为null
            cur.next = start;
            start.prev = cur;
            start = cur;

            end = prev; //尾巴修改为自己的前一个节点
        }


        return v;
    }

    public void set(int key, int value) {

        if (cache.containsKey(key)) {
            cache.get(key).data = value;
            cache.put(key, cache.get(key));
            get(key); //使用一次,移动到头部
        } else {
            Node node = new Node(key, value);
            if (cap == 1) { //容量为1时特殊处理
                start = end = node;
                cache.clear();
                cache.put(key, node);
                return;
            }

            int size = cache.size();
            if (start == null) {
                start = node;
                end = node;
                cache.put(key, node);
            } else if (size < cap) { //不需要移除尾节点,直接修改头部
                node.next = start;
                start.prev = node;
                start = node;
                cache.put(key, node);
            } else {
//                        System.out.println();
//                        System.out.println(key+" == "+value);
//                        System.out.println();


                Node last = end;
                Node lastprev = last.prev;




                end = lastprev; //设置新的尾节点
                cache.remove(last.key);
                end.next = null;
                last = null;
                node.next = start;
                start.prev = node;
                start = node; //设置新的头结点

                cache.put(key, node);
            }

            //show(start);
        }
    }

    static class Node {
        int key;
        int data;
        Node prev;
        Node next;
        public Node(int k, int d) {
            key = k;
            data = d;
        }
    }

    public void show(Node root) { //帮助打印的,本答案可以不需要
        System.out.println("");
        Node t = root;
        Set<Integer> s = new HashSet<>();
        while (t != null) {
            System.out.print(t.key + "=>" + t.data + "   ");
            t = t.next;

            //if(s.contains(t.data)) break;
        }

        System.out.println("");
    }

}


/**
 * Your Solution object will be instantiated and called as such:
 * Solution solution = new Solution(capacity);
 * int output = solution.get(key);
 * solution.set(key,value);
 */

本答案在lintcode 上相同题目没有通过全部测试用例

https://www.lintcode.com/problem/134/

后期找到原因后再修改本答案

相关推荐
qq_4298796738 分钟前
省略号和可变参数模板
开发语言·c++·算法
飞川撸码2 小时前
【LeetCode 热题100】网格路径类 DP 系列题:不同路径 & 最小路径和(力扣62 / 64 )(Go语言版)
算法·leetcode·golang·动态规划
Neil今天也要学习2 小时前
永磁同步电机参数辨识算法--IPMSM拓展卡尔曼滤波全参数辨识
单片机·嵌入式硬件·算法
yzx9910133 小时前
基于 Q-Learning 算法和 CNN 的强化学习实现方案
人工智能·算法·cnn
亮亮爱刷题3 小时前
算法练习-回溯
算法
眼镜哥(with glasses)4 小时前
蓝桥杯 国赛2024python(b组)题目(1-3)
数据结构·算法·蓝桥杯
int型码农8 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
UFIT8 小时前
NoSQL之redis哨兵
java·前端·算法
喜欢吃燃面9 小时前
C++刷题:日期模拟(1)
c++·学习·算法
SHERlocked939 小时前
CPP 从 0 到 1 完成一个支持 future/promise 的 Windows 异步串口通信库
c++·算法·promise