leetcode128 最长连续序列

思路:

使用哈希表。

实现:

 1 class Solution
 2 {
 3 public:
 4     int longestConsecutive(vector<int>& nums)
 5     {
 6         int n = nums.size();
 7         unordered_set<int> st{nums.begin(), nums.end()};
 8         int res = 0;
 9         for (auto it: st)
10         {
11             if (st.count(it - 1)) continue;
12             int num = it;
13             while (st.count(num)) num++;
14             res = max(res, num - it);
15         }
16         return res;
17     }
18 };
原文地址:https://www.cnblogs.com/wangyiming/p/14699662.html