Skip to content

Commit 898fa6f

Browse files
committed
Longest Consecutive Sequence
1 parent da9d2bf commit 898fa6f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.HashSet;
2+
3+
class Solution {
4+
public int longestConsecutive(int[] nums) {
5+
HashSet<Integer> mySet = new HashSet<Integer>();
6+
7+
for (int num : nums) {
8+
mySet.add(num);
9+
}
10+
11+
int result = 0;
12+
for (int num : mySet) {
13+
int cnt = 1;
14+
if (!mySet.contains(num - 1)) {
15+
while (mySet.contains(++num)) {
16+
++cnt;
17+
}
18+
result = Math.max(cnt, result);
19+
}
20+
}
21+
return result;
22+
}
23+
}

0 commit comments

Comments
 (0)