Skip to content

Commit 12c8c7c

Browse files
128. Longest Consecutive Sequence (java)
1 parent 3fb050b commit 12c8c7c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int longestConsecutive(int[] nums) {
3+
if (nums.length == 0) return 0;
4+
Arrays.sort(nums);
5+
int start = 0, casch = nums[0], longest = 0;
6+
for (int i = 1; i < nums.length; i++) {
7+
int nc = nums[i] , con = nc - casch;
8+
if (con == 0) {
9+
start++;
10+
} else if (con != 1) {
11+
longest = Math.max(i - start, longest);
12+
start = i;
13+
}
14+
casch = nc;
15+
}
16+
return Math.max(nums.length - start, longest);
17+
}
18+
}

0 commit comments

Comments
 (0)