-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_consecutive_sequence.cpp
More file actions
68 lines (54 loc) · 1.61 KB
/
longest_consecutive_sequence.cpp
File metadata and controls
68 lines (54 loc) · 1.61 KB
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
/*
leetcode题目,给定一个无序int数组,找出最大连续的连续整数个数
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
Your algorithm should run in O(n) complexity.
*
*/
//编译
/*
g++ -std=c++11 longest_consecutive_sequence.cpp -o longest_consecutive_sequence
*/
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
int longestConsecutive(const vector<int> &nums) {
unordered_map<int, bool> used;
//初始化
for (auto i : nums)
used[i] = false;
int longest = 0;
for (auto i : nums) {
if (used[i]) continue;
int length = 1;
used[i] = true;
for (int j = i + 1; used.find(j) != used.end(); ++j) {
used[j] = true;
++length; }
for (int j = i - 1; used.find(j) != used.end(); --j) {
used[j] = true;
++length; }
longest = max(longest, length);
}
return longest;
}
};
int main()
{
class Solution s;
int ret;
vector<int> input;
input.push_back(100);
input.push_back(4);
input.push_back(200);
input.push_back(1);
input.push_back(3);
input.push_back(2);
ret = s.longestConsecutive(input);
cout<<"result "<<ret<<endl;
return 0L;
}