-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path128.h
41 lines (36 loc) · 886 Bytes
/
128.h
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
//
// Created by 17336 on 2022/4/7.
//
#ifndef HOT100_128_H
#define HOT100_128_H
#include <vector>
#include <unordered_set>
using namespace std;
class Solution {
public:
int longestConsecutive(vector<int> &nums) {
unordered_set<int> set;
for (const auto &num: nums) {
set.insert(num);
}
int res=0;
for (int i = 0; i < nums.size(); ++i) {
if (set.count(nums[i])) {
int t = 1, x = nums[i];
while (set.count(++x)) {
++t;
set.erase(x);
}
x = nums[i];
while (set.count(--x)) {
++t;
set.erase(x);
}
set.erase(nums[i]);
res= max(res,t);
}
}
return res;
}
};
#endif //HOT100_128_H