-
Notifications
You must be signed in to change notification settings - Fork 2
/
128. Longest Consecutive Sequence.cpp
67 lines (64 loc) · 1.6 KB
/
128. Longest Consecutive Sequence.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int>mp;
for (int i=0;i<nums.size();i++){
mp.insert(nums[i]);
}
int res=0;
for (int i=0;i<nums.size();i++){
int count=1;
for (int j=nums[i]+1;mp.find(j)!=mp.end();j++){
count++;mp.erase(j);
}
for (int j=nums[i]-1;mp.find(j)!=mp.end();j--){
count++;mp.erase(j);
}res=max(res,count);
if(mp.size()<1)
break;
}
return res;
}
};
// 丫丫,小错误不断啊 yanse bianhua a
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
if(nums.empty()) return 0;
sort(nums.begin(), nums.end());
int cur=1, res=1;
for(int i=1;i<nums.size() ;i++){
if(nums[i]!=nums[i-1]){
if(nums[i]==nums[i-1]+1){
cur++;
}
else{
cur=1;
}
res=max(res,cur);
}
}
res=max(res, cur);
return res;
}
};
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> u_set(nums.begin(), nums.end());
int res = 0;
for(unordered_set<int>::iterator it=u_set.begin();
it!=u_set.end();
it++ ){
int cur = *it;
if(!u_set.count(cur-1)){
int x = cur+1, ans=1;
while(u_set.count(x)){
x++;ans++;
}
res = max(res, ans);
}
}
return res;
}
};