-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN146LRUCache.java
106 lines (84 loc) · 2.61 KB
/
N146LRUCache.java
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。
获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。
写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。
*/
package LeetCode;
import java.util.HashMap;
class Node{
public int key,val;
public Node prev,next;
public Node(int key,int val){
this.key = key;
this.val = val;
}
}
class DoubleLinkedList{
private Node head,tail;
private int length;
public DoubleLinkedList() {
this.head = new Node(0,0);
this.tail = new Node(0,0);
head.next = tail;
tail.prev = head;
length = 0;
}
public void remove(Node node){
node.prev.next = node.next;
node.next.prev = node.prev;
length--;
}
public void addFirst(Node node){
node.prev = head;
node.next = head.next;
head.next.prev = node;
head.next = node;
length++;
}
public Node removeLast(){
if(tail.prev == head) return null;
Node lastNode = tail.prev;
tail.prev = lastNode.prev;
lastNode.prev.next = tail;
length--;
return lastNode;
}
public int size(){
return length;
}
}
public class N146LRUCache {
private DoubleLinkedList doubleLinkedList;
private HashMap<Integer,Node> map;
private int cap;
public N146LRUCache(int capacity) {
this.cap = capacity;
doubleLinkedList = new DoubleLinkedList();
map = new HashMap<>();
}
public int get(int key) {
// 判断在不在map里
if(map.containsKey(key)){
int val = map.get(key).val;
put(key,val);
return val;
}
return -1;
}
public void put(int key, int value) {
// 判断在不在map里
Node newNode = new Node(key,value);
if(map.containsKey(key)){
doubleLinkedList.remove(map.get(key));
doubleLinkedList.addFirst(newNode);
map.put(key,newNode);
}else{
if(cap==doubleLinkedList.size()){
Node lastNode = doubleLinkedList.removeLast();
map.remove(lastNode.key);
}
doubleLinkedList.addFirst(newNode);
map.put(key,newNode);
}
}
}