前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java - 手写LRU(使用链表,时间复杂度O(n))

java - 手写LRU(使用链表,时间复杂度O(n))

作者头像
夹胡碰
发布2021-03-19 17:52:26
7980
发布2021-03-19 17:52:26
举报
文章被收录于专栏:程序猿~

最简单的LRU实现,底层存储采用链表结构,时间复杂度为O(n) 代码如下:

代码语言:javascript
复制
package com.jfp;

/**
 * @author jiafupeng
 * @desc
 * @create 2021/3/12 15:58
 * @update 2021/3/12 15:58
 **/
public class Test {

    public static void main(String[] args) {
        LRUCache lruCache = new LRUCache(5);
        lruCache.put(1,1);
        lruCache.put(2,2);
        lruCache.put(3,3);
        lruCache.put(4,4);
        lruCache.put(5,5);
        lruCache.put(6,6);

        System.out.println(lruCache);
        System.out.println(lruCache.get(1));
        System.out.println(lruCache.get(4));
        System.out.println(lruCache);
    }


    public static class LRUCache{

        /**
         * 长度
         */
        private int size;

        /**
         * 最大容量
         */
        private int capacity;

        /**
         * 链表头
         */
        private NodeList headNodeList;

        /**
         * 链表尾
         */
        private NodeList tailNodeList;

        public LRUCache(int capacity) {
            this.capacity = capacity;
            this.size = 0;
        }

        private static class NodeList{
            private Integer key;
            private Integer value;
            private NodeList pre;
            private NodeList next;

            public NodeList(Integer key, Integer value, NodeList pre, NodeList next) {
                this.key = key;
                this.value = value;
                this.pre = pre;
                this.next = next;
            }
        }

        public void put(int key, int val){
            NodeList getNodeList = getNodeList(key);
            if(getNodeList != null){
                getNodeList.value = val;
                return;
            }

            if(headNodeList == null){
                init(key, val);

            } else {
                tailNodeList.next = new NodeList(key, val, tailNodeList, null);
                tailNodeList = tailNodeList.next;
                size++;
            }

            if(size > capacity){
                removeHead();
            }
        }

        public Integer get(int key){
            NodeList getNodeList = getNodeList(key);
            return getNodeList == null ? null : getNodeList.value;
        }

        @Override
        public String toString(){
            NodeList nodeList = headNodeList;
            String str = "";
            while (nodeList != null){
                str += ("[" + nodeList.key + ":" + nodeList.value + "]=>");
                nodeList = nodeList.next;
            }

            return str + "null";
        }

        private void removeHead(){
            headNodeList = headNodeList.next;
        }

        private void init(int key, int value){
            headNodeList = new NodeList(key, value, null,null);
            tailNodeList = headNodeList;
            size++;
        }

        private NodeList getNodeList(int key){
            NodeList tmpNodeList = headNodeList;
            while(tmpNodeList != null){
                if(tmpNodeList.key == key){
                    // 换位置
                    moveToTail(tmpNodeList);
                    return tmpNodeList;
                }

                tmpNodeList = tmpNodeList.next;
            }

            return null;
        }

        private void moveToTail(NodeList nodeList){
            if(nodeList == tailNodeList){
                return;
            }

            if(nodeList == headNodeList){
                headNodeList = nodeList.next;
            }

            NodeList pre = nodeList.pre;
            NodeList next = nodeList.next;
            if(pre != null){
                pre.next = next;
            }

            if(next != null){
                next.pre = pre;
            }

            tailNodeList.next = nodeList;
            nodeList.pre = tailNodeList;
            nodeList.next = null;
            tailNodeList = nodeList;
        }
    }
}
输出
代码语言:javascript
复制
[2:2]=>[3:3]=>[4:4]=>[5:5]=>[6:6]=>null
null
4
[2:2]=>[3:3]=>[5:5]=>[6:6]=>[4:4]=>null
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 输出
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档