Skip to content

Commit adbc813

Browse files
committed
add solution: Longest Consecutive Sequence
1 parent e0d7d23 commit adbc813

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.*;
2+
class SolutionGotprgmer {
3+
public int longestConsecutive(int[] nums) {
4+
Set<Integer> set = new HashSet<>();
5+
for(int num:nums){
6+
set.add(num);
7+
}
8+
System.out.println(set);
9+
int answer = 0;
10+
for(int num:nums){
11+
//왼쪽으로 확장
12+
int left = 0;
13+
while(set.contains(num-(left+1))){
14+
left += 1;
15+
}
16+
17+
//오른쪽으로 확장
18+
int right = 0;
19+
while(set.contains(num+right+1)){
20+
right += 1;
21+
}
22+
System.out.println(String.valueOf(left)+" " + String.valueOf(right));
23+
answer = Math.max(answer,left+right+1);
24+
}
25+
26+
return answer;
27+
28+
}
29+
}

0 commit comments

Comments
 (0)