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