Skip to content

Commit 3430490

Browse files
PDKhanriver20s
authored andcommitted
longest-consecutive-sequence solution
1 parent 64d0880 commit 3430490

File tree

1 file changed

+30
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)