码迷,mamicode.com
首页 > Web开发 > 详细

146. LRU Cache(js)

时间:2019-06-20 22:32:21      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:number   else   dia   The   structure   remove   operation   ==   --   

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)
 */

 

146. LRU Cache(js)

标签:number   else   dia   The   structure   remove   operation   ==   --   

原文地址:https://www.cnblogs.com/xingguozhiming/p/11061779.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!