-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathsolution.cpp
48 lines (48 loc) · 1.2 KB
/
solution.cpp
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
class Solution
{
public:
int longestConsecutive(vector<int> &num)
{
map<int, int> hmap;
hmap.clear();
int n = num.size();
for(int i=0; i<n; i++)
{
hmap.insert(pair<int, int>(num[i], i));
}
int ans=0, cnt=0;
map<int, int>::iterator it;
for(int i=0; i<num.size(); i++)
{
int cur = num[i];
it = hmap.find(num[i]);
cnt++;
if(it!=hmap.end())
{
map<int, int>::iterator iter;
while(1)
{
iter = hmap.find(++cur);
if(iter==hmap.end())
break;
cnt++;
hmap.erase(iter);
}
cur=num[i];
while(1)
{
iter = hmap.find(--cur);
if(iter==hmap.end())
break;
cnt++;
hmap.erase(iter);
}
if(ans<cnt)
ans = cnt;
cnt=0;
}
cnt=0;
}
return ans;
}
};