FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/lrucache.java at master · wpande/leetcode · GitHub
wpande
leetcode
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
lrucache.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
93 lines (90 loc) · 2.37 KB
Breadcrumbs
leetcode
/
lrucache.java
Copy path
File metadata and controls
93 lines (90 loc) · 2.37 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
public
class
LRUCache
{
class
LinkNode
{
int
key
;
int
value
;
LinkNode
prior
;
LinkNode
next
;
LinkNode
(
int
ky
,
int
val
,
LinkNode
pri
,
LinkNode
nex
){
key
=
ky
;
value
=
val
;
prior
=
pri
;
next
=
nex
;
}
}
LinkNode
head
;
int
cap
;
int
cnt
;
HashMap
<
Integer
,
LinkNode
>
hm
;
public
LRUCache
(
int
capacity
) {
head
=
new
LinkNode
(
0
,
0
,
null
,
null
);
cap
=
capacity
;
cnt
=
0
;
hm
=
new
HashMap
<
Integer
,
LinkNode
>();
}
public
int
get
(
int
key
) {
LinkNode
ln
=
hm
.
get
(
key
);
if
(
ln
==
null
)
return
-
1
;
LinkNode
next
=
ln
.
next
;
ln
.
prior
.
next
=
next
;
if
(
head
.
prior
==
ln
)
head
.
prior
=
ln
.
prior
;
if
(
next
!=
null
)
next
.
prior
=
ln
.
prior
;
ln
.
prior
=
head
;
ln
.
next
=
head
.
next
;
head
.
next
=
ln
;
if
(
head
.
prior
==
null
||
head
.
prior
==
head
)
head
.
prior
=
ln
;
LinkNode
buf
=
ln
.
next
;
if
(
buf
!=
null
){
buf
.
prior
=
ln
;
}
return
ln
.
value
;
}
public
void
set
(
int
key
,
int
value
) {
if
(
hm
.
containsKey
(
key
)){
LinkNode
ln
=
hm
.
get
(
key
);
if
(
ln
.
value
!=
value
)
ln
.
value
=
value
;
LinkNode
next
=
ln
.
next
;
ln
.
prior
.
next
=
next
;
if
(
head
.
prior
==
ln
)
head
.
prior
=
ln
.
prior
;
if
(
next
!=
null
){
next
.
prior
=
ln
.
prior
;
}
ln
.
prior
=
head
;
ln
.
next
=
head
.
next
;
head
.
next
=
ln
;
LinkNode
buf
=
ln
.
next
;
if
(
buf
!=
null
){
buf
.
prior
=
ln
;
}
}
else
{
if
(
cnt
==
cap
){
LinkNode
tail
=
head
.
prior
;
if
(
tail
==
null
)
return
;
else
{
int
ky
=
tail
.
key
;
hm
.
remove
(
ky
);
cnt
--;
LinkNode
pr
=
tail
.
prior
;
pr
.
next
=
null
;
head
.
prior
=
pr
;
}
}
LinkNode
ln
=
new
LinkNode
(
key
,
value
,
head
,
head
.
next
);
LinkNode
next
=
head
.
next
;
if
(
head
.
prior
==
null
||
head
.
prior
==
head
)
head
.
prior
=
ln
;
if
(
next
!=
null
)
next
.
prior
=
ln
;
head
.
next
=
ln
;
hm
.
put
(
key
,
ln
);
cnt
++;
}
}
}
Back
|
FazBrowse Home
|
New Git URL