-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path128.cpp
More file actions
38 lines (35 loc) · 886 Bytes
/
128.cpp
File metadata and controls
38 lines (35 loc) · 886 Bytes
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
//
// 128.cpp
// LeetCode
//
// Created by 张佐玮 on 15/8/31.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Title: Longest Consecutive Sequence
//
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> unique;
int length = 1;
for (int num: nums) {
unique.insert(num);
}
for (int num: nums) {
int ceil = num + 1, floor = num - 1;
unique.erase(num);
while (unique.find(ceil) != unique.end()) {
unique.erase(ceil++);
}
while (unique.find(floor) != unique.end()) {
unique.erase(floor--);
}
length = max(length, ceil - floor - 1);
}
return length;
}
};