当前位置: 代码迷 >> 综合 >> **Leetcode 128. Longest Consecutive Sequence
  详细解决方案

**Leetcode 128. Longest Consecutive Sequence

热度:30   发布时间:2024-01-03 09:24:44.0

参考后

class Solution {
public:int longestConsecutive(vector<int>& nums) {unordered_set<int> st(cbegin(nums), cend(nums));int ret = 0;while (!st.empty()) {int num = *begin(st);int forward = num;int backward = num;int len = 1;st.erase(num);// seak forwardunordered_set<int>::iterator it;while ((it = st.find(--forward)) != cend(st)) {++len;st.erase(it);}while ((it = st.find(++backward)) != cend(st)) {++len;st.erase(it);}ret = max(ret, len);}return ret;}
};
  相关解决方案