zoukankan      html  css  js  c++  java
  • 146. LRU Cache(js)

    146. LRU Cache

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

    get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
    put(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.

    The cache is initialized with a positive capacity.

    Follow up:
    Could you do both operations in O(1) time complexity?

    Example:

    LRUCache cache = new LRUCache( 2 /* capacity */ );
    
    cache.put(1, 1);
    cache.put(2, 2);
    cache.get(1);       // returns 1
    cache.put(3, 3);    // evicts key 2
    cache.get(2);       // returns -1 (not found)
    cache.put(4, 4);    // evicts key 1
    cache.get(1);       // returns -1 (not found)
    cache.get(3);       // returns 3
    cache.get(4);       // returns 4
    题意:构建一个类和一个数据结构LRUCache,有get和put两个方法,get方法用于获取LRUCache中的值,不存在返回-1;put方法用于向LRUCache存入数值,当达到它的容量时,替换最近最少使用的
    代码如下:
    /**
     * @param {number} capacity
     */
    var LRUCache = function(capacity) {
        this.capacity=capacity;
        this.count=0;
        this.head=null;
        this.tail=null;
        this.hashTable={};
    };
    
    /** 
     * @param {number} key
     * @return {number}
     */
    LRUCache.prototype.get = function(key) {
        if(this.hashTable[key]){
            const {value}=this.hashTable[key];
            const {prev , next}=this.hashTable[key];
            if(prev) prev.next=next;
            if(next) next.prev=prev || next.prev;
            if(this.tail===this.hashTable[key]){
                this.tail=prev || this.hashTable[key];
            }
            this.hashTable[key].prev=null;
            if(this.head!==this.hashTable[key]){
                this.hashTable[key].next=this.head;
                this.head.prev=this.hashTable[key];
            }
            this.head=this.hashTable[key];
            return value;
        }
        return -1;
    };
    
    /** 
     * @param {number} key 
     * @param {number} value
     * @return {void}
     */
    LRUCache.prototype.put = function(key, value) {
        if(this.hashTable[key]){
            this.hashTable[key].value=value;
            this.get(key);
        }else{
            this.hashTable[key]={key,value,prev:null,next:null};
            if(this.head){
                this.head.prev=this.hashTable[key];
                this.hashTable[key].next=this.head;
            }
            this.head=this.hashTable[key];
            if(!this.tail){
                this.tail=this.hashTable[key];
            }
            this.count++;
        }
        if(this.count>this.capacity){
            let removeKey=this.tail.key;
            if(this.tail.prev){
                this.tail.prev.next=null;
                this.tail=this.tail.prev;
                this.hashTable[key].prev=null;
            }
            delete this.hashTable[removeKey];
            this.count--;
        }
        
    };
    
    /** 
     * Your LRUCache object will be instantiated and called as such:
     * var obj = Object.create(LRUCache).createNew(capacity)
     * var param_1 = obj.get(key)
     * obj.put(key,value)
     */
  • 相关阅读:
    linux操作提示:“Can't open file for writing”或“operation not permitted”的解决办法
    CSS中background:url(图片) 不能显示的问题
    CSS3background-size背景图片尺寸属性
    在GitHub上成果预览
    快速上手GitHub上传代码
    css布局模型(1)
    css+div浮动怎么让它在窗口大小变化时不改变位置
    node.js基于express框架搭建一个简单的注册登录Web功能
    node.js 安装使用http-server
    grunt安装与运行
  • 原文地址:https://www.cnblogs.com/xingguozhiming/p/11061779.html
Copyright © 2011-2022 走看看