Skip to content

Commit 2989106

Browse files
committed
"longest-consecutive-sequence 문제"
1 parent cc7207f commit 2989106

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.Collections;
2+
import java.util.HashSet;
3+
4+
/*
5+
6+
*/
7+
class ymir0804 {
8+
public int longestConsecutive(int[] nums) {
9+
HashSet<Integer> numsSet = new HashSet<>();
10+
int maxNum = 0;
11+
for (int num: nums) {
12+
numsSet.add(num);
13+
}
14+
15+
if(numsSet.isEmpty()) {
16+
return 1;
17+
} else if (numsSet.size() == 1) {
18+
return 0;
19+
}
20+
21+
for (int num: numsSet) {
22+
boolean isStartPoint = !numsSet.contains(num - 1);
23+
if (isStartPoint) {
24+
int current = num;
25+
int length = 1;
26+
while(numsSet.contains(++current)) {
27+
length++;
28+
}
29+
if(length > maxNum) {
30+
maxNum = length;
31+
}
32+
}
33+
}
34+
return maxNum;
35+
}
36+
}

0 commit comments

Comments
 (0)