Skip to content

Commit 27d89c9

Browse files
donghyeon95donghyeon95
authored andcommitted
feat: Longest Consecutive Sequence
DaleStudy#240
1 parent f7bf00a commit 27d89c9

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.HashSet;
2+
3+
public class Solution {
4+
public int longestConsecutive(int[] nums) {
5+
HashSet<Integer> set = new HashSet<>();
6+
for (int num : nums) {
7+
set.add(num);
8+
}
9+
10+
int maxStreak = 0;
11+
12+
for (int num : nums) {
13+
// 내가 시작 값이라면
14+
if (!set.contains(num - 1)) {
15+
int currentNum = num;
16+
int currentStreak = 1;
17+
18+
// 나로부터 연결되는 값을 찾는다.
19+
while (set.contains(currentNum + 1)) {
20+
currentNum++;
21+
currentStreak++;
22+
}
23+
24+
maxStreak = Math.max(maxStreak, currentStreak);
25+
}
26+
}
27+
28+
return maxStreak;
29+
}
30+
}
31+
32+
33+
34+

0 commit comments

Comments
 (0)