[LeetCode]LRU Cache有個問題,求大神解答

來源:互聯網
上載者:User

標籤:des   style   class   blog   code   color   

題目:

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.

這是My Code:

 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())  //如果沒有找到key17             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中插入,再在隊尾添加該key44             {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的實現機制,也是遍曆啊,按理說這兩者效率差不多,為什麼替換之後就不能通過?而替換之前能通過,求大神解答!!

  萬分感謝!!!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.