Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit43f86b6

Browse files
committed
LRU
1 parent166eb89 commit43f86b6

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

‎hash/LRUCache.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
packageAlgorithms.hash;
2+
3+
importjava.util.HashMap;
4+
5+
publicclassLRUCache {
6+
privateclassDLink {
7+
DLinkpre;
8+
DLinknext;
9+
intval;
10+
intkey;
11+
DLink(intkey,intval) {
12+
this.val =val;
13+
this.key =key;
14+
pre =null;
15+
next =null;
16+
}
17+
}
18+
19+
HashMap<Integer,DLink>map;
20+
21+
DLinkhead;
22+
DLinktail;
23+
24+
intcapacity;
25+
26+
publicvoidremoveFist() {
27+
removeNode(head.next);
28+
}
29+
30+
publicvoidremoveNode(DLinknode) {
31+
node.pre.next =node.next;
32+
node.next.pre =node.pre;
33+
}
34+
35+
// add a node to the tail.
36+
publicvoidaddToTail(DLinknode) {
37+
tail.pre.next =node;
38+
39+
node.pre =tail.pre;
40+
node.next =tail;
41+
42+
tail.pre =node;
43+
}
44+
45+
publicLRUCache(intcapacity) {
46+
map =newHashMap<Integer,DLink>();
47+
48+
// two dummy nodes. In that case, we can deal with them more conviencely.
49+
head =newDLink(-1, -1);
50+
tail =newDLink(-1, -1);
51+
head.next =tail;
52+
tail.pre =head;
53+
54+
this.capacity =capacity;
55+
}
56+
57+
publicintget(intkey) {
58+
if (map.get(key) ==null) {
59+
return -1;
60+
}
61+
62+
// update the node.
63+
DLinknode =map.get(key);
64+
removeNode(node);
65+
addToTail(node);
66+
67+
returnnode.val;
68+
}
69+
70+
publicvoidset(intkey,intvalue) {
71+
DLinknode =map.get(key);
72+
if (node ==null) {
73+
// create a node and add the key-node pair into the map.
74+
node =newDLink(key,value);
75+
map.put(key,node);
76+
}else {
77+
// update the value of the node.
78+
node.val =value;
79+
removeNode(node);
80+
}
81+
82+
addToTail(node);
83+
84+
// if the LRU is full, just remove a node.
85+
if (map.size() >capacity) {
86+
map.remove(head.next.key);
87+
removeFist();
88+
}
89+
}
90+
}

‎hash/LRUCache2.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
packageAlgorithms.hash;
2+
3+
importjava.util.LinkedHashMap;
4+
importjava.util.Map;
5+
6+
publicclassLRUCache2 {
7+
publicstaticvoidmain(String[]strs) {
8+
LRUCache2lrc2 =newLRUCache2(2);
9+
lrc2.set(1,3);
10+
lrc2.set(2,2);
11+
lrc2.set(1,4);
12+
lrc2.set(4,2);
13+
14+
System.out.println(lrc2.get(1));
15+
}
16+
17+
LinkedHashMap<Integer,Integer>map;
18+
intcapacity;
19+
20+
publicLRUCache2(finalintcapacity) {
21+
// create a map.
22+
map =newLinkedHashMap<Integer,Integer>(capacity) {
23+
/**
24+
*
25+
*/
26+
privatestaticfinallongserialVersionUID =1L;
27+
28+
protectedbooleanremoveEldestEntry(Map.Entryeldest) {
29+
returnsize() >capacity;
30+
}
31+
};
32+
this.capacity =capacity;
33+
}
34+
35+
publicintget(intkey) {
36+
Integerret =map.get(key);
37+
if (ret ==null) {
38+
return -1;
39+
}else {
40+
map.remove(key);
41+
map.put(key,ret);
42+
}
43+
44+
returnret;
45+
}
46+
47+
publicvoidset(intkey,intvalue) {
48+
map.remove(key);
49+
map.put(key,value);
50+
}
51+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp