-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path128. Longest Consecutive Sequence.cpp
More file actions
58 lines (37 loc) · 1.03 KB
/
Copy path128. Longest Consecutive Sequence.cpp
File metadata and controls
58 lines (37 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
problem link : https://leetcode.com/problems/longest-consecutive-sequence/
problem name: 128. Longest Consecutive Sequence
Status: Accepted
Author : Moahnd sakr
** */
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int maxi=0;
int len=nums.size();
if(len>0){
set<int> se;
set<int> :: iterator it;
for(int i=0;i<len;i++){
se.insert(nums[i]);
}
it=se.begin();
int answer=1;
int prev=*it;
++it;
for(;it!=se.end();it++){
int cur=*it;
if(cur-prev==1){
++answer;
}
else{
maxi=max(answer,maxi);
answer=1;
}
prev=*it;
}
maxi=max(answer,maxi);
}
return maxi;
}
};