Skip to content

Commit 394d5f3

Browse files
committed
longest-consecutive-sequence solution
1 parent 0d8c4b8 commit 394d5f3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int longestConsecutive(vector<int>& nums) {
4+
auto s = unordered_set<int>();
5+
for(int it : nums) {
6+
s.insert(it);
7+
}
8+
9+
int m = 0;
10+
for(int it : s) {
11+
if(s.count(it-1) > 0) continue;
12+
13+
int cnt = 0;
14+
while(s.count(it+cnt) > 0){
15+
cnt++;
16+
}
17+
m = max(m, cnt);
18+
}
19+
20+
return m;
21+
}
22+
};

0 commit comments

Comments
 (0)