Skip to content

Commit 9fa0ccd

Browse files
committed
Easy but im stupid hashset shit
1 parent ff230a3 commit 9fa0ccd

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

128. Longest Consecutive Sequence.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
int longestConsecutive(vector<int>& nums)
4+
{
5+
int ans = 0;
6+
set<int> hash;
7+
for(auto x:nums)
8+
{
9+
hash.insert(x);
10+
}
11+
12+
for(auto x:nums)
13+
{
14+
if(hash.contains(x-1)) //to check if this number is the start or not
15+
{
16+
continue;
17+
}
18+
else
19+
{
20+
int temp = x;
21+
int count = 0;
22+
while(hash.contains(temp))
23+
{
24+
temp = temp+1;
25+
count++;
26+
}
27+
ans = max(ans,count);
28+
}
29+
}
30+
31+
return ans;
32+
}
33+
};

0 commit comments

Comments
 (0)