日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[LeetCode]LRU Cache有个问题,求大神解答【已解决】

發布時間:2023/12/18 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [LeetCode]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.

這是我的代碼:

1 class LRUCache{ 2 public: 3 int num; 4 int max; 5 list<int> latest_key;         //用于保存使用情況,隊頭是最久未使用的,隊尾是最近使用的 6 unordered_map<int, int> cache;   //用于保存key,value 7 8 LRUCache(int capacity) { 9 num = 0; 10 max = capacity; 11 } 12 13 int get(int key) { 14 unordered_map<int, int>::iterator it = cache.find(key); 15 list<int>::iterator iter; 16 if (it == cache.end())  //如果沒有找到key 17 return -1; 18 else            //如果找到了key,就在對應的最近latest隊列里面修改key的位置,先把key所在的位置刪除,再把key放到隊尾 19 { 20 iter = latest_key.begin(); 21 while (*iter != key) 22 iter++; 23 latest_key.erase(iter); 24 latest_key.push_back(key); 25 return it->second; 26 } 27 } 28 29 void set(int key, int value) { 30 unordered_map<int, int>::iterator it = cache.find(key); 31 list<int>::iterator iter; 32 if (it != cache.end())  //如果要插入的已經有key存在,就先在優先隊列里面找到key出現的位置,刪除,再把key插入隊尾 33 { 34 it->second = value; 35 iter = latest_key.begin(); 36 while (*iter != key) 37 iter++; 38 latest_key.erase(iter); 39 latest_key.push_back(key); 40 } 41 else            //如果要插入的不存在 42 { 43 if (num<max)  //如果不超過cache容量,則直接在cahe中插入,再在隊尾添加該key 44 { 45 num++; 46 cache.insert(std::pair< int, int >(key, value)); 47 latest_key.push_back(key); 48 } 49 else      //如果cache已經滿了,則根據隊頭元素,在cache刪除對應鍵值,再在隊列中刪除這個隊頭,之后,把新要插入的鍵值插入cache中,把新key插入隊尾 50 { 51 int latest = latest_key.front(); 52 cache.erase(latest); 53 latest_key.pop_front(); 54 cache.insert(std::pair< int, int >(key, value)); 55 latest_key.push_back(key); 56 } 57 } 58 } 59 };

  當我把代碼中出現:

1 iter = latest_key.begin(); 2 while (*iter != key) 3 iter++;

  部分替換為:

1 iter=find(latest_key.begin(),latest_key.end(),key);

  就會報錯:

Time Limit Exceeded

Last executed input: 2048,[set(1178,3401),set(903,6060).....

  我大致查了一下find的實現機制,也是遍歷啊,按理說這兩者效率差不多,為什么替換之后就不能通過?而替換之前能通過,求大神解答!!

  萬分感謝!!!

  在Leetcode上問,已經得到答案:

?

  之前的那個算法效率確實不高,壓線過的,修改了原有代碼,增加了一個unordered_map<int, list<int>iterator>用來索引list,可以使時間復雜度降到O(1):

1 class LRUCache{ 2 private: 3 unordered_map<int, int> cache; 4 unordered_map<int, list<int>::iterator> find_key; 5 list<int> latest_key; 6 int max; 7 public: 8 LRUCache(int capacity) : max(capacity){ 9 10 } 11 12 int get(int key) { 13 if (cache.find(key) == cache.end()){ 14 return -1; 15 } 16 latest_key.erase(find_key[key]); 17 latest_key.push_front(key); 18 find_key[key] = latest_key.begin(); 19 return cache[key]; 20 } 21 22 void set(int key, int value) { 23 if (cache.find(key) == cache.end()) { 24 if (cache.size() >= max) { 25 cache.erase(latest_key.back()); 26 latest_key.pop_back(); 27 cache[key] = value; 28 latest_key.push_front(key); 29 find_key[key] = latest_key.begin(); 30 } 31 else { 32 cache[key] = value; 33 latest_key.push_front(key); 34 find_key[key] = latest_key.begin(); 35 } 36 } 37 else { 38 cache[key] = value; 39 latest_key.erase(find_key[key]); 40 latest_key.push_front(key); 41 find_key[key] = latest_key.begin(); 42 } 43 } 44 };

?

轉載于:https://www.cnblogs.com/yanqi0124/p/3806680.html

總結

以上是生活随笔為你收集整理的[LeetCode]LRU Cache有个问题,求大神解答【已解决】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。