Skip to content

Commit 81d7df4

Browse files
committed
longest consecutive sequence solution
1 parent 635b515 commit 81d7df4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <bits/stdc++.h>
2+
3+
class Solution {
4+
public:
5+
int longestConsecutive(vector<int>& nums) {
6+
if(nums.size() == 0)
7+
return 0;
8+
9+
sort(nums.begin(), nums.end());
10+
11+
int ans = 1, cnt = 1;
12+
for(int i = 1; i < nums.size(); i++) {
13+
if(nums[i] == nums[i - 1])
14+
continue;
15+
if(nums[i] - 1 == nums[i - 1])
16+
cnt++;
17+
else
18+
cnt = 1;
19+
ans = max(cnt, ans);
20+
}
21+
22+
return ans;
23+
}
24+
};

0 commit comments

Comments
 (0)