Skip to content

Commit 21339d9

Browse files
committed
longest-consecutive-sequence solution
1 parent 309f321 commit 21339d9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int longestConsecutive(int[] nums) {
3+
// 연속된 숫자 길이 파악
4+
// 연속된 거 어떻게?
5+
if(nums.length == 0 || nums == null) return 0;
6+
7+
int result = 0;
8+
9+
Arrays.sort(nums);
10+
11+
int maxLen = 1;
12+
int len = 1;
13+
14+
for(int i = 1; i < nums.length; i++) {
15+
// 중복 체크
16+
if(nums[i] == nums[i-1]) {
17+
continue;
18+
}else if(nums[i] == nums[i-1] + 1) {
19+
len++;
20+
maxLen = Math.max(maxLen, len);
21+
}else{
22+
len = 1;
23+
}
24+
}
25+
26+
return maxLen;
27+
}
28+
}

0 commit comments

Comments
 (0)