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
读题:模拟一个LRU(最近最少使用队列),实现它的put和get
分析:
1.无论是put还是get,只要是在队列中命中了此对象,就把它移到队列最前面,因为他是最近用到的。需要注意是如果对象在队尾,处理下边界情况
2.get没有命中,不需要做处理,put没有命中,需要新增此对象到队列中。
3.put方法需要注意队列空时初始化head,put时size+1,队列满时,删除队尾一个元素,对象移到对头的处理跟get一样
解题:
定义队列元素:
public class Node{
Node pre;
Node next;
int key;
int val;
};
形成双向链表
ac代码:
class LRUCache {
int capacity;
int curCount = 0;
Node head;
Node tail;
public LRUCache(int capacity) {
this.capacity = capacity;
}
public int get(int key) {
if(head == null){
return -1;
}
else if(head.key == key){
return head.val;
}
else{
Node cur = head.next;
while(cur != null){
if(cur.key == key){//在队列中找到了key。下面把它变为head
if(tail == cur){//如果是尾部,先更换尾部
tail = cur.pre;
}
cur.pre.next = cur.next;
if(cur.next != null){
cur.next.pre = cur.pre ;
}
cur.next = head;
head.pre = cur;
head=cur;
return head.val;
}
else{
cur = cur.next;
}
}
return -1;
}
}
public void put(int key, int value) {
if(head == null){
head = new Node();
head.key = key;
head.val = value;
curCount++;
tail = head;
}
else if(head.key == key){
head.val = value;
return;
}
else{
Node cur = head;
while(cur != null){
if(cur.key == key){//在队列中找到了key。下面把它变为head
if(tail == cur){//如果是尾部,先更换尾部
tail = cur.pre;
}
cur.pre.next = cur.next;
if(cur.next != null){
cur.next.pre = cur.pre ;
}
cur.next = head;
head.pre = cur;
head = cur;
head.val = value;
return;
}
else{
cur = cur.next;
}
}
//队列不包含key,添加进去
if(curCount == capacity){//队列满了,去掉最后一个
if(capacity == 1){
head.key = key;
head.val = value;
}
else{
tail = tail.pre;
tail.next.pre = null;
tail.next = null;
Node temp = new Node();
temp.key = key;
temp.val = value;
temp.next = head;
head.pre = temp;
head = temp;
}
}
else{//否则直接加到队头
Node temp = new Node();
temp.key = key;
temp.val = value;
temp.next = head;
head.pre = temp;
head = temp;
curCount++;
}
}
}
class Node{
Node pre;
Node next;
int key;
int val;
}
}
/**
* 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);
*/
这是一个纯模拟的写法,没有做什么优化,也不是O(1)解法,待续。。。
今天想了一下,实现LRU应该没有其他取巧的办法,还是要通过双向链表来实现,于是想到了LinkedHashMap,查到LinkedHashMap是可以实现LRU模型的,那么就可以用LinkedHashMap来解这道题。
class LRUCache {
class LRUMap<K,V> extends LinkedHashMap<K, V> implements Map<K, V>{
int capacity;
public LRUMap(int capacity) {
super(16, (float) 0.75, true);
this.capacity = capacity;
}
@Override
protected boolean removeEldestEntry(Map.Entry<K,V> eldest){
if(size() > capacity){
return true;
}
return false;
}
}
Map<Integer,Integer> lruMap ;
public LRUCache(int capacity) {
lruMap = new LRUMap<Integer,Integer>(capacity);
}
public int get(int key) {
if(lruMap.containsKey(key)){
return lruMap.get(key);
}
else {
return -1;
}
}
public void put(int key, int value) {
lruMap.put(key, value);
}
}
因为LinkedHashMap的get和put都是O(1)时间复杂度,因此这种解法是满足要求的。LinkedHashMap如何实现LRU见另一篇对LinkedHaspMap的源码阅读。