-
Notifications
You must be signed in to change notification settings - Fork 0
/
longestConsecutiveSequence.cpp
53 lines (49 loc) · 1.15 KB
/
longestConsecutiveSequence.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 <unordered_map>
//寻找无序数组中最长连续序列的长度,要求O(n)。
//优先考虑hash_map,每个数的上下寻找,已找过的在之后可以忽略
using namespace std;
int longestConsecutive(vector<int>& nums) {
if (nums.empty())
return 0;
int cur = 0;
int max = 0;
unordered_map<int, bool> used;
for (int i = 0; i < nums.size(); ++i)
used[nums[i]] = false;
for (int i = 0; i < nums.size(); ++i) {
if (used[nums[i]])
continue;
else {
used[nums[i]] = true;
cur = 1;
for (int j = nums[i] + 1; used.find(j) != used.end(); ++j) {
cur++;
used[j] = true;
}
for (int j = nums[i] - 1; used.find(j) != used.end(); --j) {
cur++;
used[j] = true;
}
if (cur > max)
max = cur;
}
}
return max;
}
int main()
{
vector<int> nums;
int tmp, res;
char ch;
cout << "Please input the array:";
while (cin >> tmp) {
nums.push_back(tmp);
if ((ch = cin.get()) == '\n')
break;
}
res = longestConsecutive(nums);
cout << "The longest consecutive sequence in the array is: " << res << endl;
return 0;
}