-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path146.lru-cache-solution2.java
More file actions
46 lines (41 loc) · 1.55 KB
/
146.lru-cache-solution2.java
File metadata and controls
46 lines (41 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* @lc app=leetcode id=146 lang=java
*
* [146] LRU Cache
*/
// @lc code=start
/*
LinkedHashMap:
constructor : (int initial capacity, float load factor, boolean accessorder)
load factor: load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.
When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is,
internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.
default load factor (.75) offers a good tradeoff between time and space costs.
order: order in which keys were inserted into the map (insertion-order), from least recently -> most recently
re-insert key will not affect the order
* It is not syschronized, Map m = Collections.synchronizedMap(new LinkedHashMap(...));
*/
class LRUCache extends LinkedHashMap<Integer, Integer> {
private int capacity;
public LRUCache(int capacity) {
super(capacity, 0.75F, true);
this.capacity = capacity;
}
public int get(int key) {
return super.getOrDefault(key, -1);
}
public void put(int key, int value) {
super.put(key, value);
}
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
return size() > capacity;
}
}
/**
* 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);
*/
// @lc code=end