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 ff230a3 commit 9fa0ccdCopy full SHA for 9fa0ccd
128. Longest Consecutive Sequence.cpp
@@ -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
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