File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments