-
Notifications
You must be signed in to change notification settings - Fork 0
/
128.cpp
53 lines (48 loc) · 1.13 KB
/
128.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
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int longestConsecutive(vector<int>& nums) {
if (nums.size() == 0) return 0;
map<int, int> numcount;
int res = 1;
int maxlengh = 1;
for (int i = 0; i < nums.size(); i++) {
int curr_num = nums[i];
numcount[curr_num] += 1;
}
for (auto iter = numcount.begin(); iter != numcount.end(); iter++) {
auto next = iter;
auto nextnext = next;
nextnext++;
if (nextnext == numcount.end()) {
break;
} else {
next++;
}
if(iter->first + 1 == next->first) {
maxlengh += 1;
if (maxlengh > res) {
res = maxlengh;
}
} else {
maxlengh = 1;
}
}
return res;
}
int main() {
vector<int> test;
test.push_back(0);
test.push_back(3);
test.push_back(7);
test.push_back(2);
test.push_back(5);
test.push_back(8);
test.push_back(4);
test.push_back(6);
test.push_back(0);
test.push_back(1);
printf("%d\n", longestConsecutive(test));
return 0;
}