Skip to content

Commit 4018dd3

Browse files
committed
add solution for longest-consecutive-sequence
1 parent 642cae5 commit 4018dd3

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int longestConsecutive(int[] nums) {
5+
//int[] 를 정렬
6+
Arrays.sort(nums);
7+
//maxlength를 갱신하는 식으로
8+
int maxLength = 0;
9+
int curLength = 0 ;
10+
long prev = Long.MIN_VALUE;
11+
long cur = Long.MIN_VALUE;
12+
13+
14+
for(int i =0 ; i < nums.length; i++){
15+
16+
//공통
17+
prev = cur;
18+
cur = nums[i];
19+
System.out.println("i: "+ i);
20+
21+
//System.out.println("cur: "+ cur);
22+
//System.out.println("prev: "+ prev);
23+
if (curLength == 0) curLength++;
24+
25+
if (cur == prev + 1){
26+
curLength++;
27+
maxLength = Math.max(maxLength, curLength);
28+
//System.out.println("curLength : " + curLength);
29+
//System.out.println("maxLength : " + maxLength);
30+
}else{//다르면
31+
System.out.println("여기");
32+
33+
34+
if (cur == prev){
35+
maxLength = Math.max(maxLength, curLength);
36+
}else{
37+
maxLength = Math.max(maxLength, curLength);
38+
curLength = 0;
39+
}
40+
41+
//System.out.println("curLength : " + curLength);
42+
//System.out.println("maxLength : " + maxLength);
43+
}
44+
45+
46+
47+
48+
49+
50+
}
51+
52+
return maxLength;
53+
54+
}
55+
}

0 commit comments

Comments
 (0)