FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/longestconsecutivesequence.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
/
longestconsecutivesequence.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
36 lines (36 loc) · 1.12 KB
Breadcrumbs
leetcode
/
longestconsecutivesequence.java
Copy path
File metadata and controls
36 lines (36 loc) · 1.12 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
public
class
Solution
{
class
Interval
{
int
left
;
int
right
;
Interval
(
int
l
,
int
r
){
left
=
l
;
right
=
r
;
}
}
public
int
longestConsecutive
(
int
[]
nums
) {
HashMap
<
Integer
,
Interval
>
hm
=
new
HashMap
<
Integer
,
Interval
>();
int
len
=
nums
.
length
;
if
(
len
<
1
)
return
0
;
int
res
=
1
;
for
(
int
i
=
0
;
i
<
len
;
i
++)
if
(!
hm
.
containsKey
(
nums
[
i
])){
int
left
=
nums
[
i
];
int
right
=
left
;
if
(
nums
[
i
]!=
Integer
.
MIN_VALUE
&&
hm
.
containsKey
(
nums
[
i
]-
1
))
left
=
hm
.
get
(
nums
[
i
]-
1
).
left
;
if
(
nums
[
i
]!=
Integer
.
MAX_VALUE
&&
hm
.
containsKey
(
nums
[
i
]+
1
))
right
=
hm
.
get
(
nums
[
i
]+
1
).
right
;
hm
.
put
(
nums
[
i
],
new
Interval
(
left
,
right
));
if
(
right
-
left
+
1
>
res
)
res
=
right
-
left
+
1
;
Interval
buf
=
hm
.
get
(
left
);
buf
.
left
=
left
;
buf
.
right
=
right
;
buf
=
hm
.
get
(
right
);
buf
.
left
=
left
;
buf
.
right
=
right
;
}
return
res
;
}
}
Back
|
FazBrowse Home
|
New Git URL