C++實現(xiàn)LeetCode(146.近最少使用頁面置換緩存器)
[LeetCode] 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.
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
這道題讓我們實現(xiàn)一個 LRU 緩存器,LRU 是 Least Recently Used 的簡寫,就是最近最少使用的意思。那么這個緩存器主要有兩個成員函數(shù),get 和 put,其中 get 函數(shù)是通過輸入 key 來獲得 value,如果成功獲得后,這對 (key, value) 升至緩存器中最常用的位置(頂部),如果 key 不存在,則返回 -1。而 put 函數(shù)是插入一對新的 (key, value),如果原緩存器中有該 key,則需要先刪除掉原有的,將新的插入到緩存器的頂部。如果不存在,則直接插入到頂部。若加入新的值后緩存器超過了容量,則需要刪掉一個最不常用的值,也就是底部的值。具體實現(xiàn)時我們需要三個私有變量,cap, l和m,其中 cap 是緩存器的容量大小,l是保存緩存器內(nèi)容的列表,m是 HashMap,保存關鍵值 key 和緩存器各項的迭代器之間映射,方便我們以 O(1) 的時間內(nèi)找到目標項。
然后我們再來看 get 和 put 如何實現(xiàn),get 相對簡單些,我們在 HashMap 中查找給定的 key,若不存在直接返回 -1。如果存在則將此項移到頂部,這里我們使用 C++ STL 中的函數(shù) splice,專門移動鏈表中的一個或若干個結(jié)點到某個特定的位置,這里我們就只移動 key 對應的迭代器到列表的開頭,然后返回 value。這里再解釋一下為啥 HashMap 不用更新,因為 HashMap 的建立的是關鍵值 key 和緩存列表中的迭代器之間的映射,get 函數(shù)是查詢函數(shù),如果關鍵值 key 不在 HashMap,那么不需要更新。如果在,我們需要更新的是該 key-value 鍵值對兒對在緩存列表中的位置,而 HashMap 中還是這個 key 跟鍵值對兒的迭代器之間的映射,并不需要更新什么。
對于 put,我們也是現(xiàn)在 HashMap 中查找給定的 key,如果存在就刪掉原有項,并在頂部插入新來項,然后判斷是否溢出,若溢出則刪掉底部項(最不常用項)。代碼如下:
class LRUCache{ public: LRUCache(int capacity) { cap = capacity; } int get(int key) { auto it = m.find(key); if (it == m.end()) return -1; l.splice(l.begin(), l, it->second); return it->second->second; } void put(int key, int value) { auto it = m.find(key); if (it != m.end()) l.erase(it->second); l.push_front(make_pair(key, value)); m[key] = l.begin(); if (m.size() > cap) { int k = l.rbegin()->first; l.pop_back(); m.erase(k); } } private: int cap; list<pair<int, int>> l; unordered_map<int, list<pair<int, int>>::iterator> m; };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/146
類似題目:
Design Compressed String Iterator
參考資料:
https://leetcode.com/problems/lru-cache/
http://www.cnblogs.com/TenosDoIt/p/3417157.html
https://leetcode.com/problems/lru-cache/discuss/46285/unordered_map-list
到此這篇關于C++實現(xiàn)LeetCode(146.近最少使用頁面置換緩存器)的文章就介紹到這了,更多相關C++實現(xiàn)近最少使用頁面置換緩存器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Qt的QChartView實現(xiàn)縮放和放大功能
QCustomPlot是一個小型的Qt畫圖標類,支持繪制靜態(tài)曲線、動態(tài)曲線、多重坐標曲線,柱狀圖,蠟燭圖,這篇文章主要介紹了Qt的QChartView實現(xiàn)縮放和放大功能,需要的朋友可以參考下2022-09-09C語言編寫基于TCP和UDP協(xié)議的Socket通信程序示例
這篇文章主要介紹了C語言編寫基于TCP和UDP協(xié)議的Socket通信程序示例,其中TCP的客戶端與服務器端采用多線程實現(xiàn),需要的朋友可以參考下2016-03-03