We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cc7207f commit 2989106Copy full SHA for 2989106
longest-consecutive-sequence/ymir0804.java
@@ -0,0 +1,36 @@
1
+import java.util.Collections;
2
+import java.util.HashSet;
3
+
4
+/*
5
6
+*/
7
+class ymir0804 {
8
+ public int longestConsecutive(int[] nums) {
9
+ HashSet<Integer> numsSet = new HashSet<>();
10
+ int maxNum = 0;
11
+ for (int num: nums) {
12
+ numsSet.add(num);
13
+ }
14
15
+ if(numsSet.isEmpty()) {
16
+ return 1;
17
+ } else if (numsSet.size() == 1) {
18
+ return 0;
19
20
21
+ for (int num: numsSet) {
22
+ boolean isStartPoint = !numsSet.contains(num - 1);
23
+ if (isStartPoint) {
24
+ int current = num;
25
+ int length = 1;
26
+ while(numsSet.contains(++current)) {
27
+ length++;
28
29
+ if(length > maxNum) {
30
+ maxNum = length;
31
32
33
34
+ return maxNum;
35
36
0 commit comments