-
Notifications
You must be signed in to change notification settings - Fork 0
/
128. Longest Consecutive Sequence.cpp
65 lines (48 loc) · 1.44 KB
/
128. Longest Consecutive Sequence.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
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include<vector>
#include<unordered_set>
using namespace std;
// Input: nums = [100,4,200,1,3,2]
// Output: 4
class Solution {
public:
int countSequence(vector<int>& nums,int num){
int counter = 1;
while(true){
if(mySet.find(num+1) != mySet.end()){
num++;
counter++;
}
else{
return counter;
}
}
}
int longestConsecutive(vector<int>& nums) {
if(nums.size()==0){
return 0;
}
int longest = 1;
for(int i=0;i<nums.size();i++){
mySet.insert(nums[i]);
}
unordered_set<int> sequenceSet;
for(int i=0;i<nums.size();i++){
if(mySet.find(nums[i]-1) == mySet.end() && sequenceSet.find(nums[i]-1) == sequenceSet.end()){
sequenceSet.insert(nums[i]-1);
longest = max(countSequence(nums,nums[i]),longest);
}
}
return longest;
}
private:
unordered_set<int> mySet;
};
int main(){
Solution solution;
vector<int> input = {0,3,7,2,5,8,4,6,0,1};
// vector<int> input = {100,4,200,1,3,2};
int answer = solution.longestConsecutive(input);
cout << "Answer: " << answer << endl;
cout << "Hello World" << endl;
}