class Solution { public: /** * * @param num int整型vector * @return int整型 */ int longestConsecutive(vector<int>& num) { if (num.size() == 0) return 0; if (num.size() == 1) return 1; map<int, int> m; for (int i = 0; i < num.size(); i++) { m[num[i]] = i; } int k = 1; int res = 1; int first = m.begin()->first; int second ; for (auto i = m.begin()++; i != m.end(); i++) { second = i->first; if (first + 1 == second) { k++; } else { k = 1; } res = max(res, k); first = second; } return res; } };