128. 最长连续序列

128. 最长连续序列_第1张图片

int longestConsecutive(vector &nums) {
    unordered_set s;
    for (auto it:nums)
        s.insert(it);

    int count = 0;
    for (auto it:s)
        if (!s.count(it - 1)) {
            int num = it;
            int tempCount = 1;
            while (s.count(num + 1))
                num++, tempCount++;
            count = max(count, tempCount);
        }
    return count;
}

你可能感兴趣的:(128. 最长连续序列)