LRU算法的JavaScript实现

LRU就是Least Recently Used,即最近最少使用,是一种常用的页面置换算法,将最近长时间未使用的页面淘汰,其实也很简单,就是要将不受欢迎的页面及时淘汰,不让它占着茅坑不拉shit,浪费资源。

其核心就是利用栈,进行操作,其中主要有两项操作,get和put

  1. get
    get时,若栈中有值则将该值的key提到栈顶,没有时则返回null
  2. put
  • 栈未满时,若栈中有要put的key,则更新此key对应的value,并将该键值提到栈顶,若无要put的key,直接入栈
  • 栈满时,若栈中有要put的key,则更新此key对应的value,并将该键值提到栈顶;若栈中没有put的key 时,去掉栈底元素,将put的值入到栈顶

代码已通过leetcode测试

/**
 * @param {number} capacity
 */
var LRUCache = function(capacity) {
    this.capacity=capacity
    this.cache=[]
    
};

/** 
 * @param {number} key
 * @return {number}
 */
LRUCache.prototype.get = function(key) {
    let el
    for(let i=0;i<this.cache.length;i++){
        if(this.cache[i].key===key){
            //当键值存在时,将键值移到栈顶
            var tail=this.cache.splice(i,1)
            this.cache.push(tail[0])
            
            return tail[0].val 
        }
    }

    return -1
 
};

/** 
 * @param {number} key 
 * @param {number} value
 * @return {void}
 */
LRUCache.prototype.put = function(key, value) {
        var ca={
            'key':key,
            'val':value,
        }
        //当缓存中存在键值时,更新键值,并将键值放在栈顶
        for(let i=0;i<this.cache.length;i++){
            if(this.cache[i]['key']===key){
                this.cache[i]=ca
                let el=this.cache.splice(i,1)
                this.cache.push(el[0])
 
                return null
            }
        }
        //此时为缓存中没有键值
        if(this.cache.length<this.capacity){
            //当缓存未满时直接入
            this.cache.push(ca)
            
        }else{
            //当缓存满了,去掉栈底元素,将新元素放在栈顶
            this.cache.shift()
            this.cache.push(ca)
        }
   
        
    }
    

/** 
 * Your LRUCache object will be instantiated and called as such:
 * var obj = new LRUCache(capacity)
 * var param_1 = obj.get(key)
 * obj.put(key,value)
 */
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
LRU(Least Recently Used)算法是一种常见的缓存淘汰策略,用于在缓存容量不足时,淘汰最近最少使用的缓存块。下面是LRU算法的C语言实现: ``` #include <stdio.h> #include <stdlib.h> typedef struct Node { int key; int value; struct Node *next; struct Node *prev; } Node; typedef struct LRU { int size; int capacity; Node *head; Node *tail; } LRU; LRU* newLRU(int capacity) { LRU *lru = (LRU*)malloc(sizeof(LRU)); lru->size = 0; lru->capacity = capacity; lru->head = NULL; lru->tail = NULL; return lru; } void deleteNode(LRU *lru, Node *node) { if (node == lru->head) { lru->head = node->next; } else { node->prev->next = node->next; } if (node == lru->tail) { lru->tail = node->prev; } else { node->next->prev = node->prev; } free(node); } void moveToHead(LRU *lru, Node *node) { if (node == lru->head) { return; } if (node == lru->tail) { lru->tail = node->prev; lru->tail->next = NULL; } else { node->prev->next = node->next; node->next->prev = node->prev; } node->next = lru->head; node->prev = NULL; lru->head->prev = node; lru->head = node; } void set(LRU *lru, int key, int value) { Node *node = lru->head; while (node != NULL) { if (node->key == key) { node->value = value; moveToHead(lru, node); return; } node = node->next; } if (lru->size >= lru->capacity) { deleteNode(lru, lru->tail); lru->size--; } Node *newNode = (Node*)malloc(sizeof(Node)); newNode->key = key; newNode->value = value; newNode->next = NULL; newNode->prev = NULL; if (lru->head == NULL) { lru->head = newNode; lru->tail = newNode; } else { newNode->next = lru->head; lru->head->prev = newNode; lru->head = newNode; } lru->size++; } int get(LRU *lru, int key) { Node *node = lru->head; while (node != NULL) { if (node->key == key) { moveToHead(lru, node); return node->value; } node = node->next; } return -1; // 缓存未命中 } ``` LRU算法的主要思想是将最近访问的缓存块移动到链表头部,当缓存容量不足时,淘汰链表尾部的缓存块。实现中用双向链表和哈希表来存储缓存块,双向链表用于记录最近访问顺序,哈希表用于快速查找缓存块。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值