[Algorithm] Leet_460_LRU_Cache
Leet_460_LRU_Cache 접근방식
[Algorithm] Leet_460_LRU_Cache
Leet_460_LRU_Cache
문제 링크
https://leetcode.com/problems/lru-cache/description/
카테고리
자료구조
접근 방식
linked hashmap을 사용하여 구현
코드
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
package ver2.Leet_460_LRU_Cache;
import java.util.*;
class LRUCache {
public LinkedHashMap<Integer, Integer> linkedHashMap;
public int capacity;
public LRUCache(int capacity) {
this.capacity = capacity;
this.linkedHashMap = new LinkedHashMap<>(capacity, 0.75f, true);
}
public int get(int key) {
return linkedHashMap.getOrDefault(key, -1);
}
public void put(int key, int value) {
linkedHashMap.put(key, value);
if (linkedHashMap.size() > capacity) {
int leastUsedKey = linkedHashMap.keySet().iterator().next();
linkedHashMap.remove(leastUsedKey);
}
}
}
This post is licensed under CC BY 4.0 by the author.