zoukankan      html  css  js  c++  java
  • LRU Cache

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

    get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
    set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

    import java.util.HashMap;
    
    public class LRUCache {
    
        private class CacheObject {
            public CacheObject pre;
            public CacheObject next;
            public int key;
            public int value;
            public CacheObject(int key,int value) {
                this.key = key;
                this.value = value;
                this.next = null;
                this.pre = null;
            }
        }
    
        public HashMap table = new HashMap ();
    
        public CacheObject head;
    
        public CacheObject tail;
    
        public int capacity;
    
        private boolean isTail(CacheObject co){
            if (tail==null) {
                return false;
            }
            return tail==co;
        }
    
        private void fixTail(CacheObject co) {
            if (tail==head){
                return;
            }
            if (isTail(co)){
                tail = co.pre;
                tail.next = null;
            }
        }
    
        private void move2head(CacheObject co){
            if (head==co){
                return;
            }
            if (tail==head){
                CacheObject oldHead = head;
                head = co;
                head.next = oldHead;
                head.pre = null;
                oldHead.pre = co;
                oldHead.next = null;
                return;
            }
            if (head != null){
                CacheObject pre = co.pre;
                CacheObject next = co.next;
                if (pre!=null && next !=null){
                    pre.next = next;
                    next.pre = pre;
                }
                head.pre = co;
                co.next = head;
                co.pre = null;
            }
            head = co;
        }
    
        public LRUCache(int capacity) {
            this.capacity = capacity;
        }
    
        public int get(int key) {
            if (table.containsKey(key)) {
                CacheObject added = (CacheObject)table.get(key);
                if (head!=added){
                    fixTail(added);
                    move2head(added);
                }
                return added.value;
            }
            return -1;
        }
    
    
        public void set(int key, int value) {
            if (table.containsKey(key)){
                CacheObject tmp = (CacheObject) table.get(key);
                fixTail(tmp);
                move2head(tmp);
                tmp.value = value;
                return;
            }
            CacheObject added = new CacheObject(key, value);
            if (this.capacity == table.size()) {
                table.remove(tail.key);
                fixTail(tail);
            }
            if (table.size()==0) {
                tail = added;
                head = added;
            } else {
                move2head(added);
            }
            table.put(key,added);
        }
    }
  • 相关阅读:
    前端与算法 leetcode 28.实现 strStr()
    前端与算法 leetcode 27.移除元素
    npm钉钉脚手架,支持考勤信息获取
    VSCode 使用Settings Sync同步配置(最新版教程,非常简单)
    VSCode 远程开发(带免密)
    vscode保存代码,自动按照eslint规范格式化代码设置
    matplotlib 3D数据-【老鱼学matplotlib】
    matplotlib等高线图-【老鱼学matplotlib】
    matplotlib柱状图-【老鱼学matplotlib】
    matplotlib散点数据-【老鱼学matplotlib】
  • 原文地址:https://www.cnblogs.com/23lalala/p/3506828.html
Copyright © 2011-2022 走看看