Computer Science/C++
[C++] LeetCode 128. Longest Consecutive Sequence
focalpoint
2023. 8. 18. 06:48
https://leetcode.com/problems/longest-consecutive-sequence/
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int ret = 0;
unordered_set<int> num_set(nums.begin(), nums.end());
for (int num : nums) {
// not found
if (num_set.find(num - 1) == num_set.end()) {
int cnt = 0;
while (num_set.find(num) != num_set.end()) {
num += 1;
cnt += 1;
}
ret = max(ret, cnt);
}
}
return ret;
}
};