-
Notifications
You must be signed in to change notification settings - Fork 1
/
128.cpp
39 lines (34 loc) · 952 Bytes
/
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
// 128. Longest Consecutive Sequence - https://leetcode.com/problems/longest-consecutive-sequence
#include "bits/stdc++.h"
using namespace std;
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_map<int, bool> hashmap;
for (int num : nums) {
hashmap[num] = false;
}
int result = 0;
for (auto it : hashmap) {
if (it.second) { continue; }
int ans = 1;
int num = it.first;
int step = 1;
while (hashmap.count(num - step)) {
++ans;
hashmap[num - step++] = true;
}
step = 1;
while (hashmap.count(num + step)) {
++ans;
hashmap[num + step++] = true;
}
result = max(result, ans);
}
return result;
}
};
int main() {
ios::sync_with_stdio(false);
return 0;
}