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

设计LRU缓存结构

http://www.nowcoder.com/practice/e3769a5f49894d49b871c09cadd13a61

HashMap用来查询,双向链表用来维护最新,效率高于LinkedHashMap
import java.util.*;


public class Solution {
    /**
     * lru design
     * @param operators int整型二维数组 the ops
     * @param k int整型 the k
     * @return int整型一维数组
     */
    //双向链表使用hash表加快访问速度
    class Node{
        int val,key;
        Node pre;
        Node next; 
        public Node(int key,int val){
            this.val=val;
            this.key=key;
            this.pre=null;
            this.next=null;
        }
    }
    
    public int[] LRU (int[][] operators, int k) {
        // write code here
        Node head=new Node(-1,-1);
        Node tail=new Node(-1,-1);
        head.next=tail;
        tail.pre=head;
        Map<Integer,Node> LRU=new HashMap<>();
        List<Integer> res=new ArrayList<>();
        for(int i=0;i<operators.length;i++){
            if(operators[i][0]==1){
                Integer key=new Integer(operators[i][1]);
                Integer value=new Integer(operators[i][2]);
                set(LRU,key,value,k, head, tail);
            }
            else if(operators[i][0]==2){
                int result=get(LRU,operators[i][1],k, head, tail);
                res.add(result);
            }
            
        }
        int []rest=new int[res.size()];
        for(int i=0;i<res.size();i++){
            rest[i]=res.get(i);
        }
        return rest;
        
    }
    public void set(Map<Integer,Node> LRU,Integer key,Integer value,int k,Node head,Node tail){
       if(LRU.containsKey(key)){
           Node tmp=LRU.get(key);
           tmp.pre.next=tmp.next;
           tmp.next.pre=tmp.pre;
           insertTOHead(tmp,head,tail);
           tmp.val=value;
       }
       else{
           if(LRU.size()>=k){
               LRU.remove(tail.pre.key);
               Node p1=tail.pre.pre;
               p1.next=tail;
               tail.pre.pre=null;
               tail.pre.next=null;
               tail.pre=p1;
           }
           Node tmp=new Node(key,value);
           LRU.put(key,tmp);
           insertTOHead(tmp,head,tail);
       }
    }
    public int get(Map<Integer,Node> LRU,Integer key,int k,Node head,Node tail){
        if(!LRU.containsKey(key)){
            return -1;
        }
        else{
            Node tmp=LRU.get(key);
            int res=tmp.val;
            tmp.pre.next=tmp.next;
            tmp.next.pre=tmp.pre;
            insertTOHead(tmp,head,tail);
            return res;
        }
    }
    public void insertTOHead(Node tmp,Node head,Node tail){
        tmp.next=head.next;
        tmp.next.pre=tmp;
        head.next=tmp;
        tmp.pre=head;
    }
}
全部评论

相关推荐

压力很大,面试官全程高压,问的问题不难,但是没有任何反馈,很慌张,也无算法。实习问了20分钟,一直问我你们做的有什么用,总时长一小时1.学校都有什么课程2.spring的ioc原理以及优点3.除了解耦还知道什么?4.springboot与spring区别,二者的源码看过没?Tomcat了解嘛?有没有具体看过5.spring的bean,面试官一直在重复一个思想问我懂不懂,完全没听过6.mybatis是干什么的?ibatis用过没?平常怎么写SQL?完全不写嘛?7.设计一个分布式双十一秒杀系统(前端,网关,缓存,数据库防超卖全设计)8.怎么做限流9.缓存与数据库一致性,你做异步要用户等你嘛?10.负载均衡怎么做11.多数据中心还是单数据中心,如果出现没卖完怎么做(到这完全不会了,面试官直接说换个话题吧)12.平常读书吗?13.上过哲学课嘛?14.兴趣爱好有没有15.对ai的看法16.来深圳有问题嘛?17.为什么不考研18.上大学带给了你什么?你提升在哪里,有没有具体的例子?反问:1.现在手机都有应用市场,应用宝怎么盈利?除了手机应用市场还是有人用,现在在做跨端,微软都有合作,之后会进军mac,主要做游戏,腾讯本身就是游戏大户。2.面试表现?整体评价一下会给到反馈。面完直接变HR面,今天HR面后,已经转为录用评估了,来牛客许个愿,暑期现在还没什么面试,希望能拿个offer之后再考虑要不要留在手子吧。
nunuking:三面压力这么大吗,面试的会议约了多长时间呀
面试问题记录
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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