国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

leetcode做題筆記146. LRU 緩存

這篇具有很好參考價值的文章主要介紹了leetcode做題筆記146. LRU 緩存。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

請你設(shè)計(jì)并實(shí)現(xiàn)一個滿足??LRU (最近最少使用) 緩存?約束的數(shù)據(jù)結(jié)構(gòu)。

實(shí)現(xiàn)?LRUCache?類:

  • LRUCache(int capacity)?以?正整數(shù)?作為容量?capacity?初始化 LRU 緩存
  • int get(int key)?如果關(guān)鍵字?key?存在于緩存中,則返回關(guān)鍵字的值,否則返回?-1?。
  • void put(int key, int value)?如果關(guān)鍵字?key?已經(jīng)存在,則變更其數(shù)據(jù)值?value?;如果不存在,則向緩存中插入該組?key-value?。如果插入操作導(dǎo)致關(guān)鍵字?jǐn)?shù)量超過?capacity?,則應(yīng)該?逐出?最久未使用的關(guān)鍵字。

函數(shù)?get?和?put?必須以?O(1)?的平均時間復(fù)雜度運(yùn)行。

思路一:雙向鏈表

c語言解法

struct LRUInfo{
    int val;
    int value;
    struct LRUInfo* pre;
    struct LRUInfo* next;
};

typedef struct {
    int top;
    int total;
    struct LRUInfo * head;
    struct LRUInfo * rear;
    struct LRUInfo lruinfo[10001];
} LRUCache;

LRUCache* lRUCacheCreate(int capacity) {
    LRUCache* obj = calloc(1, sizeof(LRUCache));
    obj->total = capacity;
    obj->head = calloc(1, sizeof(struct LRUInfo));
    obj->rear = calloc(1, sizeof(struct LRUInfo));
    obj->head->next = obj->rear;
    obj->rear->pre = obj->head;
    return obj;
}

int lRUCacheGet(LRUCache* obj, int key) {
    if (obj->lruinfo[key].val == 1) {
        obj->lruinfo[key].pre->next = obj->lruinfo[key].next;
        obj->lruinfo[key].next->pre = obj->lruinfo[key].pre;
        obj->rear->pre->next = obj->lruinfo + key;
        obj->lruinfo[key].pre = obj->rear->pre;
        obj->lruinfo[key].next = obj->rear;
        obj->rear->pre = obj->lruinfo + key;
        return obj->lruinfo[key].value;
    }
    return -1;
}

void lRUCachePut(LRUCache* obj, int key, int value) {
    if (obj->lruinfo[key].val == 0 && obj->top < obj->total) {
        (obj->top)++;
        obj->rear->pre->next = obj->lruinfo + key;
        obj->lruinfo[key].pre = obj->rear->pre;
        obj->lruinfo[key].next = obj->rear;
        obj->lruinfo[key].val = 1;
        obj->lruinfo[key].value = value;
        obj->rear->pre = obj->lruinfo + key;
    } else if (obj->lruinfo[key].val == 1){
        obj->lruinfo[key].pre->next = obj->lruinfo[key].next;
        obj->lruinfo[key].next->pre = obj->lruinfo[key].pre;
        obj->rear->pre->next = obj->lruinfo + key;
        obj->lruinfo[key].pre = obj->rear->pre;
        obj->lruinfo[key].next = obj->rear;
        obj->lruinfo[key].value = value;
        obj->rear->pre = obj->lruinfo + key;
    } else if (obj->lruinfo[key].val == 0 && obj->top >= obj->total && obj->head->next != NULL) {
        obj->lruinfo[key].val = 1;
        obj->lruinfo[key].value = value;
        obj->rear->pre->next = obj->lruinfo + key;
        obj->lruinfo[key].pre = obj->rear->pre;
        obj->lruinfo[key].next = obj->rear;
        obj->rear->pre = obj->lruinfo + key;
        obj->head->next->val = 0;
        obj->head->next = obj->head->next->next;
        obj->head->next->pre = obj->head;       
    }
    return;
}

void lRUCacheFree(LRUCache* obj) {
    free(obj);
}

/**
 * Your LRUCache struct will be instantiated and called as such:
 * LRUCache* obj = lRUCacheCreate(capacity);
 * int param_1 = lRUCacheGet(obj, key);
 
 * lRUCachePut(obj, key, value);
 
 * lRUCacheFree(obj);
*/

分析:

本題要實(shí)現(xiàn)LRU緩存實(shí)現(xiàn)雙向鏈表的各個操作后即可解決,刪除方法利用前驅(qū)節(jié)點(diǎn)的指針才能滿足O(1)的時間復(fù)雜度,get方法利用前驅(qū)節(jié)點(diǎn)達(dá)到O(1)的時間復(fù)雜度

總結(jié):

本題考察對LRU緩存的實(shí)現(xiàn),考慮到各個方法的實(shí)現(xiàn)的時間復(fù)雜度要求在O(1),所以采用雙向鏈表保證時間復(fù)雜度,最后實(shí)現(xiàn)各個方法即可解決文章來源地址http://www.zghlxwxcb.cn/news/detail-730682.html

到了這里,關(guān)于leetcode做題筆記146. LRU 緩存的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【LeetCode-中等題】146. LRU 緩存

    【LeetCode-中等題】146. LRU 緩存

    LRU緩存是什么:LRU緩存機(jī)制,你想知道的這里都有 實(shí)現(xiàn) LRU 緩存算法 map —key存 integer value存鏈表節(jié)點(diǎn) ListNode 存 next 和prev 引用 以及 存 key 和value 具體值 圖解:官方圖解 步驟: 定義一個自定義的雙向鏈表節(jié)點(diǎn)類 DLinkedNode,該類包含 key 和 value 字段,并且具有 prev 和 next 指針

    2024年02月10日
    瀏覽(30)
  • 【數(shù)據(jù)結(jié)構(gòu)】LRU緩存的簡單模擬實(shí)現(xiàn)(leetcode力扣146LRU緩存)

    【數(shù)據(jù)結(jié)構(gòu)】LRU緩存的簡單模擬實(shí)現(xiàn)(leetcode力扣146LRU緩存)

    LRU是Least Recently Used的縮寫,意思是最近最少使用,它是一種Cache替換算法。 Cache的容量有限,因此當(dāng)Cache的容量用完后,而又有新的內(nèi)容需要添加進(jìn)來時, 就需要挑選并舍棄原有的部分內(nèi)容,從而騰出空間來放新內(nèi)容。LRU Cache 的替換原則就是將最近最少使用的內(nèi)容替換掉。

    2024年02月03日
    瀏覽(23)
  • Leetcode 146. LRU 緩存(Hashmap+雙鏈表)

    請你設(shè)計(jì)并實(shí)現(xiàn)一個滿足 LRU (最近最少使用) 緩存 約束的數(shù)據(jù)結(jié)構(gòu)。 實(shí)現(xiàn) LRUCache 類: ● LRUCache(int capacity) 以 正整數(shù) 作為容量 capacity 初始化 LRU 緩存 ● int get(int key) 如果 key 存在于緩存中,則返回的值,否則返回 -1 。 ● void put(int key, int value) 如果 key 已

    2024年02月16日
    瀏覽(34)
  • 二刷LeetCode--146.LRU緩存(C++版本),必會題目

    本題思路:因?yàn)樾枰涗浽氐某鋈腠樞?并且每次訪問之后需要將該節(jié)點(diǎn)提到最前面,因此需要使用雙向鏈表(單鏈表不方便刪除操作),而為了可以在常量時間復(fù)雜度內(nèi)找到對應(yīng)的元素,我們需要使用哈希表,將每一個插入的元素在哈希表中進(jìn)行記錄.哈希表的key就是插入的key,而哈希

    2024年02月13日
    瀏覽(22)
  • [力扣146. LRU 緩存 ](https://leetcode.cn/problems/lru-cache/description/)

    力扣146. LRU 緩存 使用LinkedHashmap(HashMap的子類,能夠記住插入數(shù)據(jù)的順序). LRU是Lease Recently User的縮寫,意思是最近 最少使用。比如設(shè)計(jì)一個文件緩存系統(tǒng),每個文件有自己的大小和訪問時間,文件緩存系統(tǒng)有總的大小,當(dāng)往這個文件系統(tǒng)中放入新的文件時,如果發(fā)現(xiàn)超出文件

    2024年02月11日
    瀏覽(54)
  • 【leetcode100-035】【鏈表/哈希鏈表】LRU緩存

    【題干】 請你設(shè)計(jì)并實(shí)現(xiàn)一個滿足??LRU (最近最少使用) 緩存?約束的數(shù)據(jù)結(jié)構(gòu)。 實(shí)現(xiàn)? LRUCache ?類: LRUCache(int capacity) ?以? 正整數(shù) ?作為容量? capacity ?初始化 LRU 緩存 int get(int key) ?如果? key ?存在于緩存中,則返回的值,否則返回? -1 ?。 void put(int key, in

    2024年02月01日
    瀏覽(57)
  • 146. LRU 緩存

    請你設(shè)計(jì)并實(shí)現(xiàn)一個滿足 LRU (最近最少使用) 緩存 約束的數(shù)據(jù)結(jié)構(gòu)。 實(shí)現(xiàn) LRUCache 類: LRUCache(int capacity) 以 正整數(shù) 作為容量 capacity 初始化 LRU 緩存 int get(int key) 如果 key 存在于緩存中,則返回的值,否則返回 -1 。 void put(int key, int value) 如果 key 已經(jīng)存在,

    2024年02月12日
    瀏覽(28)
  • (力扣記錄)146. LRU 緩存

    數(shù)據(jù)類型 :鏈表 時間復(fù)雜度: O(1) 空間復(fù)雜度: O(N) 代碼實(shí)現(xiàn):

    2024年01月18日
    瀏覽(21)
  • 【LeetCode熱題100】打卡第33天:環(huán)形鏈表&LRU緩存

    【LeetCode熱題100】打卡第33天:環(huán)形鏈表&LRU緩存

    大家好,我是知識汲取者,歡迎來到我的LeetCode熱題100刷題專欄! 精選 100 道力扣(LeetCode)上最熱門的題目,適合初識算法與數(shù)據(jù)結(jié)構(gòu)的新手和想要在短時間內(nèi)高效提升的人,熟練掌握這 100 道題,你就已經(jīng)具備了在代碼世界通行的基本能力。在此專欄中,我們將會涵蓋各種

    2024年02月15日
    瀏覽(21)
  • 146. LRU Cache最近最少使用 (LRU) 緩存 Least Recently Used (LRU) cache.

    Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the LRUCache class: LRUCache(int capacity) Initialize the LRU cache with positive size capacity. int get(int key) Return the value of the key if the key exists, otherwise return -1. void put(int key, int value) Update the value of the key if the key exists. O

    2024年02月10日
    瀏覽(34)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包