链表-设计LRU缓存结构

题目描述:

代码实现:这里记录了根据LRU算法原理最直接理解的代码实现。

import java.util.*;

//存储输入内容,记录访问权值
class CounterInfo {
    int key;
    int value;
    int times;//代表key对应的权值,值越小优先级越高
    public CounterInfo(int key, int value) {
        this.key = key;
        this.value = value;
        this.times = 0;
    }
}

public class Solution {
    ArrayList<CounterInfo> intarr;
    int maxsize;
    public Solution(int capacity) {
        // write code here
        intarr = new ArrayList<CounterInfo>(0);
        this.maxsize = capacity;
    }


    //除了key以外的元素都更新times++
    public void refreshTimes(int key) {
        Iterator infoit = intarr.iterator();
        while (infoit.hasNext()) {
            CounterInfo nowinfo = (CounterInfo)infoit.next();
            if (nowinfo.key != key) {
                nowinfo.times++;
            }
        }
    }


    /*
    1.遍历列表看是否存在key,如果存在则返回相应的value,如果不存在返回-1
    2.如果存在目标key,并且目标key对应权值不为0,更新目标key对应的权值为0,
      其他key对应权值都+1
    3.如果存在目标key,但是目标key对应权值为0,列表内所有权值不做改变
    */
    public int get(int key) {
        boolean isfind = false;
        boolean isrefresh = false;
        int resValue = -1;
        //查找intarr中是否存在key
        Iterator infoit = intarr.iterator();
        while (infoit.hasNext()) {
            CounterInfo nowinfo = (CounterInfo)infoit.next();
            if (nowinfo.key == key) {
                isfind = true;
                resValue = nowinfo.value;
                if (nowinfo.times != 0) {
                    isrefresh = true;
                    nowinfo.times = 0;
                } else {
                    isrefresh = false;
                }
            }
        }
        if (isfind) {
            if (isrefresh) {
                //更新其他info的times
                this.refreshTimes(key);
            }
            return resValue;
        } else {
            return -1;
        }
    }


    /*
    1.看是否存在key,如果存在更新key对应的value和权值=0
    2.如果不存在:
        2.1 列表满:选择权值最大的元素,修改key,value,权值=0;其他元素权值+1
        2.2 列表未满:列表添加新的CounterInfo对象;其他元素权值+1
    */
    public void set(int key, int value) {
        //先看是否存在
        boolean isfind = false;
        //查找intarr中是否存在key
        Iterator infoit = intarr.iterator();
        while (infoit.hasNext()) {
            CounterInfo nowinfo = (CounterInfo)infoit.next();
            if (nowinfo.key == key) {
                isfind = true;
                //更新value
                nowinfo.value = value;
                if (nowinfo.times != 0) {
                    nowinfo.times = 0;
                    this.refreshTimes(key);
                }
            }
        }

        if (!isfind) {
            //判断是否达到最大长度
            if (intarr.size() == maxsize) {
                //找到最久未访问的元素,更新key,value,times
                infoit = intarr.iterator();

                //找到最大time
                int maxtime = 0;
                while (infoit.hasNext()) {
                    CounterInfo nowinfo = (CounterInfo)infoit.next();
                    maxtime = maxtime > nowinfo.times ? maxtime : nowinfo.times;
                }

                //根据最大time更新key-value值
                infoit = intarr.iterator();
                while (infoit.hasNext()) {
                    CounterInfo nowinfo = (CounterInfo)infoit.next();
                    if (nowinfo.times == maxtime) {
                        nowinfo.times = 0;
                        nowinfo.key = key;
                        nowinfo.value = value;
                    }
                }
            } else {
                CounterInfo newinfo = new CounterInfo(key, value);
                intarr.add(newinfo);
            }
            this.refreshTimes(key);
        }

    }
}

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

刷题链接:

设计LRU缓存结构_牛客题霸_牛客网

相关推荐

  1. -LRU缓存

    2024-05-25 21:38:22       35 阅读
  2. 】Leetcode 146. LRU 缓存【中等】

    2024-05-25 21:38:22       45 阅读
  3. LRU缓存(哈希+双

    2024-05-25 21:38:22       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-25 21:38:22       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-25 21:38:22       101 阅读
  3. 在Django里面运行非项目文件

    2024-05-25 21:38:22       82 阅读
  4. Python语言-面向对象

    2024-05-25 21:38:22       91 阅读

热门阅读

  1. memcpy的使⽤和模拟实现

    2024-05-25 21:38:22       36 阅读
  2. 通关!游戏设计之道Day14

    2024-05-25 21:38:22       36 阅读
  3. python 多线程处理图片

    2024-05-25 21:38:22       32 阅读
  4. unity 制作app实现底部导航栏和顶部状态栏

    2024-05-25 21:38:22       38 阅读
  5. 什么是js

    2024-05-25 21:38:22       31 阅读
  6. 怎么使Ajax设为同步和异步

    2024-05-25 21:38:22       33 阅读
  7. C:技术面试总结

    2024-05-25 21:38:22       35 阅读
  8. Android NDK系列(二)NativeActivity工作流程

    2024-05-25 21:38:22       37 阅读
  9. OSI七层模型和TCP/IP四层模型的区别

    2024-05-25 21:38:22       28 阅读