Skip to content

Commit 55ac21d

Browse files
committed
longest consecutive sequence solved
1 parent 9a24a1f commit 55ac21d

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
1-
import java.util.HashSet;
2-
import java.util.Set;
3-
41
class Solution {
52
public int longestConsecutive(int[] nums) {
63
Set<Integer> numSet = new HashSet<>();
4+
int max = 0;
75

86
for(int num : nums) {
97
numSet.add(num);
108
}
119

12-
int longestSize = 0;
13-
1410
for(int num : numSet) {
15-
if(!numSet.contains(num - 1)) {
16-
int current = num;
17-
int count = 1;
11+
if(numSet.contains(num - 1)) {
12+
continue;
13+
}
1814

19-
while(numSet.contains(current + 1)) {
20-
count++;
21-
current++;
22-
}
15+
int count = 1;
16+
int curNum = num;
2317

24-
longestSize = Math.max(count, longestSize);
18+
while(numSet.contains(curNum + 1)) {
19+
count++;
20+
curNum++;
2521
}
22+
23+
max = Math.max(max, count);
2624
}
2725

28-
return longestSize;
26+
return max;
2927
}
3028
}

top-k-frequent-elements/mintheon.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ public int[] topKFrequent(int[] nums, int k) {
2121

2222
return answer;
2323
}
24-
}
24+
}

two-sum/mintheon.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ public int[] twoSum(int[] nums, int target) {
1717

1818
return new int[]{};
1919
}
20-
}
20+
}

0 commit comments

Comments
 (0)