題目地址:
https://leetcode-cn.com/probl...
題目描述:
運用你所掌握的數據結構,設計和實現一個 LRU (最近最少使用) 緩存機制。它應該支持以下操作: 獲取數據 get 和 寫入數據 put 。
獲取數據 get(key) - 如果密鑰 (key) 存在於緩存中,則獲取密鑰的值(總是正數),否則返回 -1。
寫入數據 put(key, value) - 如果密鑰不存在,則寫入其數據值。當緩存容量達到上限時,它應該在寫入新數據之前刪除最近最少使用的數據值,從而爲新的數據值留出空間。
進階:
你是否可以在 O(1) 時間複雜度內完成這兩種操作?
示例:
LRUCache cache = new LRUCache( 2 / 緩存容量 / );
cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // 返回 1
cache.put(3, 3); // 該操作會使得密鑰 2 作廢
cache.get(2); // 返回 -1 (未找到)
cache.put(4, 4); // 該操作會使得密鑰 1 作廢
cache.get(1); // 返回 -1 (未找到)
cache.get(3); // 返回 3
cache.get(4); // 返回 4
解答:
這一題就是實現一個最近最少使用的緩存。我們根據操作系統的定義可以想出一個比較樸素的方法:
利用隊列(基於鏈表的隊列),每放入一個數據就入隊列(尾部插入),而淘汰一個數據就出隊列(從頭部淘汰)。
而更新一個數據就把這個數據先刪除,然後再入隊列。這樣就能夠保證最近最少使用。
這種做法,如果永遠不更新數據,每次插入的數據都一定是不在隊列裏的,那麼能夠保證put()是O(1)。
但是無法保證get()是O(1),也無法保證更新數據時是O(1),因爲這兩個操作必然要遍歷隊列。
如何保證get()是O(1)複雜度呢?利用HashSet,可以用常數時間查詢是否有這個元素?而如何使得更新也是O(1)呢?
如果我們的鏈表是雙向鏈表,並且維護表頭,表尾,再利用一個HashMap定位到這個鏈表節點(key是這個節點的key,value是這個節點的引用),就可以使得更新也是O(1)。
不過這裏可以把HashSet和HashMap合起來用,只用HashMap即可。因爲可以通過HashMap.get(key) == null?來判斷是否有這個節點。
java ac代碼:
class LRUCache {
//使用HashMap+雙向鏈表才能達到O(1)
HashMap<Integer,Node>map;
int capacity;
int size;
Node head,tail;
public LRUCache(int capacity) {
this.capacity = capacity;
map = new HashMap(capacity*2);
head = new Node(0,0);
tail = new Node(0,0);
head.next = tail;
tail.pre = head;
}
public int get(int key) {
Node node = map.get(key);
if(node != null)
{
if(node.next != tail)
{
node.pre.next = node.next;
node.next.pre = node.pre;
node.next = tail;
node.pre = tail.pre;
tail.pre.next = node;
tail.pre = node;
}
return node.value;
}
return -1;
}
public void put(int key, int value) {
if(get(key) != -1)
{
tail.pre.value = value;
return;
}
if(size < capacity)
size++;
else
{
Node node = head.next;
map.remove(node.key);
head.next = node.next;
node.next.pre = head;
}
Node node = new Node(key,value);
map.put(key,node);
node.next = tail;
node.pre = tail.pre;
tail.pre.next = node;
tail.pre = node;
}
class Node
{
int key,value;
Node pre,next;
public Node(int key,int value)
{
this.key = key;
this.value = value;
}
}
}
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/