Skip to content

Commit b664c85

Browse files
committed
prgressing longestConsecutive
1 parent 2c63bc1 commit b664c85

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.HashSet;
2+
import java.util.Iterator;
3+
4+
public class Geegong {
5+
6+
public int longestConsecutive(int[] nums) {
7+
8+
HashSet<Integer> setOfNums = new HashSet<>();
9+
int start = 0;
10+
int end = 0;
11+
12+
// initialize
13+
for (int num : nums) {
14+
setOfNums.add(num);
15+
start = Math.min(start, num);
16+
end = Math.max(end, num);
17+
}
18+
19+
Integer longest = 0;
20+
Integer current = 0;
21+
while (start <= end) {
22+
23+
iterate(setOfNums, start, current);
24+
longest = Math.max(longest, current);
25+
}
26+
}
27+
28+
public Integer iterate(HashSet<Integer> hashSet, Integer start, Integer currentLength) {
29+
30+
if (hashSet.contains(start)) {
31+
currentLength++;
32+
iterate(hashSet, start+1, currentLength);
33+
34+
} else {
35+
// currentLength 를 리턴해야 하나..
36+
return start;
37+
}
38+
39+
}
40+
41+
}
42+

0 commit comments

Comments
 (0)