Java面试编程题-2:设计LRU缓存结构

Java面试编程题-2:设计LRU缓存结构

题目描述
设计LRU缓存结构,该结构在构造时确定大小,假设大小为K,并有如下两个功能:
set(key, value):将记录(key, value)插入该结构;
get(key):返回key对应的value值。

题目要求
set和get方法的时间复杂度为O(1);
某个key的set或get操作一旦发生,认为这个key的记录成了最常使用的;
当缓存的大小超过K时,移除最不经常使用的记录,即set或get最久远的;
若opt=1,接下来两个整数x, y,表示set(x, y),
若opt=2,接下来一个整数x,表示get(x),若x未出现过或已被移除,则返回-1;
对于每个操作2,输出一个答案。

示例
输入:
[[1,1,1],[1,2,2],[1,3,2],[2,1],[1,4,4],[2,2]],3
返回值:
[1,-1]
说明
第一次操作后:最常使用的记录为(“1”, 1)
第二次操作后:最常使用的记录为(“2”, 2),(“1”, 1)变为最不常用的
第三次操作后:最常使用的记录为(“3”, 2),(“1”, 1)还是最不常用的
第四次操作后:最常用的记录为(“1”, 1),(“2”, 2)变为最不常用的
第五次操作后:大小超过了3,所以移除此时最不常使用的记录(“2”, 2),加入记录(“4”, 4),并且为最常使用的记录,然后(“3”, 2)变为最不常使用的记录

import java.util.*;

class Node {
    int key, val;
    Node next,pre;

    Node(int key, int val){
        this.key = key;
        this.val = val;
        next = null;
        pre = null;
    }
}

public class Solution {

    Node head = new Node(-1, -1);
    Node tail = new Node(-1, -1);
    int l = 0, K = 0;

    public int get(int key){

        Node p = head;
        while(p.next != tail){
            p = p.next;
            if(p.key == key){
                p.next.pre = p.pre;
                p.pre.next = p.next;
                p.pre = head;
                p.next = head.next;
                head.next.pre = p;
                head.next = p;
                return p.val;
            }
        }
        return -1;
    }

    public void set(int key, int val){

        Node p = head;
        while(p.next != tail){
            p = p.next;
            if(p.key == key){
                p.val = val;
                return;
            }
        }

        Node n = new Node(key, val);
        head.next.pre = n;
        n.next = head.next;
        n.pre = head;
        head.next = n;
        if(++l > K){
            tail.pre.pre.next = tail;
            tail.pre = tail.pre.pre;
        }

    }

    public int[] LRU (int[][] operators, int k) {
        // write code here
        head.next = tail;
        tail.pre = head;
        K = k;

        int len = (int)Arrays.stream(operators).filter(x->x[0] == 2).count();
        int[] ans = new int[len];

        for(int i = 0, j = 0; i < operators.length; i++){
            if(operators[i][0] == 1){
                set(operators[i][1], operators[i][2]);
            }
            else if(operators[i][0] == 2){
                ans[j++] = get(operators[i][1]);
            }
        }
        return ans;
    }

    public static void main(String[] args){
        int[][] operators = {
                {1,1,1},
                {1,2,2},
                {1,3,2},
                {2,1},
                {1,4,4},
                {2,2}
        };
        int K = 3;
        Solution sl = new Solution();
        int[] ans = sl.LRU(operators, K);
        for(int i : ans){
            System.out.println(i + " ");
        }
        System.out.println();
    }
}

输出结果

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值