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 476d86f commit 1620167Copy full SHA for 1620167
longest-consecutive-sequence/dohee789.java
@@ -0,0 +1,25 @@
1
+/*
2
+https://leetcode.com/problems/longest-consecutive-sequence/
3
+ */
4
+class Solution {
5
+ public int longestConsecutive(int[] nums) {
6
+ Set<Integer> set = new HashSet<>();
7
+ for(int num: nums){
8
+ set.add(num);
9
+ }
10
+
11
+ int maxLength = 0;
12
+ for(int num: set){
13
+ if(!set.contains(num-1)){
14
+ int currentNum = num;
15
+ int length = 1;
16
+ while(set.contains(currentNum+1)){
17
+ currentNum++;
18
+ length++;
19
20
+ maxLength = Math.max(maxLength,length);
21
22
23
+ return maxLength;
24
25
+}
0 commit comments