题解 | #设计LRU缓存结构#

设计LRU缓存结构

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

import java.util.*;


public class Solution {

     HashMap<Integer,MyNode> map;
     int size;
     MyNode begin = new MyNode(-1);
     MyNode end = new MyNode(-1);
    public Solution(int capacity) {
        // write code here
        map = new HashMap<Integer,MyNode>();
        size = capacity;
        begin.next = end;
        end.last = begin;
    }

    public int get(int key) {
        // write code here
        if(map.containsKey(key)){
             MyNode temp = map.get(key);
             temp.next.last = temp.last;
             temp.last.next = temp.next;
             temp.last = end.last;
             end.last.next = temp;
             end.last = temp;
             temp.next = end;
             return temp.val;
        }
        return -1;
    }

    public void set(int key, int value) {
        // write code here
        if(map.containsKey(key)){
             MyNode one = map.get(key);
             one.val = value;
             one.last.next = one.next;
             one.next.last = one.last;
             one.last = end.last;
             end.last.next = one;
             end.last = one;
             one.next = end;
        }else{
             if(map.size() < size){
             MyNode one = new MyNode(value,key,end,end.last);
             map.put(key,one);
             one.last = end.last;
             end.last.next = one;
             end.last = one;
             one.next = end;
          }else{
             MyNode one = new MyNode(value,key,end,end.last);
             map.put(key,one);
             one.last = end.last;
             end.last.next = one;
             end.last = one;
             one.next = end;
             MyNode two = map.remove(begin.next.key);
             begin.next = two.next;
             two.next.last = begin;
          }
        }
    }
    class MyNode{
         MyNode next;
         MyNode last;
         int key;
         int val;
         public MyNode(int val){
              this.val = val;
         }
         public MyNode(int val,int key){
              this.val = val;
              this.key = key;
         }
         public MyNode(int val,int key , MyNode next,MyNode last){
              this.val = val;
              this.key = key;
              this.next = next;
              this.last = last;
         }
    }
}

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

相关推荐

不愿透露姓名的神秘牛友
07-09 16:15
我应届生,去年10月份开始在这家公司实习,到今年10月份正好一年想(实习+试用期),在想要不要提前9月份就离职,这样好找工作些,但又差一个月满一年,又怕10月份国庆回来离职,容易错过了下半年的金九银十,到年底容易gap到年后
小破站_程序员YT:说这家公司不好吧,你干了快一年 说这家公司好吧,你刚毕业就想跑路说你不懂行情吧,你怕错过金九银十说 你懂行情吧,校招阶段在实习,毕业社招想换工作 哥们,我该怎么劝你留下来呢
应届生,你找到工作了吗
点赞 评论 收藏
分享
07-09 12:12
门头沟学院 Java
5月底投简历7月初开奖收获秋招第一个offer,虽然白菜价,但至少能保底了
土木转行ing:土木博士想转图像,最后拿了 tp 提前批 sp 最低档,感觉性价比不高
TP-LINK开奖132人在聊
点赞 评论 收藏
分享
屌丝逆袭咸鱼计划:心态摆好,man,晚点找早点找到最后都是为了提升自己好进正职,努力提升自己才是最关键的😤难道说现在找不到找的太晚了就炸了可以鸡鸡了吗😤早实习晚实习不都是为了以后多积累,大四学长有的秋招进的也不妨碍有的春招进,人生就这样
点赞 评论 收藏
分享
06-10 23:36
已编辑
首都经济贸易大学 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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