Skip to content

Latest commit

 

History

History
18 lines (17 loc) · 395 Bytes

128. Longest Consecutive Sequence.md

File metadata and controls

18 lines (17 loc) · 395 Bytes
class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set<int> h(nums.begin(), nums.end());
        int ans = 0;
        for (int num : nums) {
            if (!h.count(num - 1)) {
                int len = 0;
                while (h.count(num++)) ++len;
                ans = max(ans, len);
            }
        }
        return ans;
    }
};