Skip to content

Commit 615e84a

Browse files
committed
solve longest consecutive sequence
1 parent dd0a5aa commit 615e84a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

longest-consecutive-sequence/sora0319.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
import java.util.*;
2+
// 2번째 푼 코드
3+
class Solution {
4+
public int longestConsecutive(int[] nums) {
5+
Set<Integer> set = new HashSet<>();
6+
for (int n : nums) {
7+
set.add(n);
8+
}
9+
10+
int cntMax = 0;
11+
for (int n : set) {
12+
if (!set.contains(n - 1)) {
13+
int end = n;
14+
while (set.contains(end + 1)) {
15+
end++;
16+
}
17+
cntMax = Math.max(cntMax, end - n + 1);
18+
}
19+
}
20+
21+
return cntMax;
22+
}
23+
}
224

25+
// 처음 풀어본 코드
326
class Solution {
427
public int longestConsecutive(int[] nums) {
528
Set<Integer> checkList = new HashSet<>();

0 commit comments

Comments
 (0)