solution.md 4.9 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 最长连续序列
2

每日一练社区's avatar
每日一练社区 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<p>给定一个未排序的整数数组 <code>nums</code> ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。</p>

<p>请你设计并实现时间复杂度为 <code>O(n)</code><em> </em>的算法解决此问题。</p>

<p> </p>

<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong>nums = [100,4,200,1,3,2]
<strong>输出:</strong>4
<strong>解释:</strong>最长数字连续序列是 <code>[1, 2, 3, 4]。它的长度为 4。</code></pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong>nums = [0,3,7,2,5,8,4,6,0,1]
<strong>输出:</strong>9
</pre>

<p> </p>

<p><strong>提示:</strong></p>

<ul>
	<li><code>0 <= nums.length <= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>

每日一练社区's avatar
每日一练社区 已提交
32
<p>以下<span style="color:red">错误</span>的选项是?</p>
每日一练社区's avatar
每日一练社区 已提交
33 34

## aop
35

每日一练社区's avatar
每日一练社区 已提交
36
### before
37

每日一练社区's avatar
每日一练社区 已提交
38
```cpp
每日一练社区's avatar
每日一练社区 已提交
39 40 41
#include <bits/stdc++.h>
using namespace std;
```
每日一练社区's avatar
每日一练社区 已提交
42

每日一练社区's avatar
每日一练社区 已提交
43
### after
44

每日一练社区's avatar
每日一练社区 已提交
45
```cpp
每日一练社区's avatar
每日一练社区 已提交
46 47 48 49 50 51 52 53 54 55 56 57
int main()
{
    Solution sol;
    int res;
    vector<int> vec = {0, 3, 7, 2, 5, 8, 4, 6, 0, 1};
    res = sol.longestSequence(vec);
    cout << res;
    return 0;
}
```

## 答案
58

每日一练社区's avatar
每日一练社区 已提交
59
```cpp
每日一练社区's avatar
每日一练社区 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
class Solution
{
public:
    int longestConsecutive(vector<int> &nums)
    {
        unordered_set<int> hash;
        for (const int &num : nums)
        {
            hash.insert(num);
        }
        int ans = 0;
        while (!hash.empty())
        {
            int cur = *(hash.begin());
            hash.erase(cur);
            int next = cur + 1, prev = cur - 1;
            while (hash.count(next))
            {
                hash.erase(next++);
            }
            while (hash.count(prev))
            {
                hash.erase(prev--);
            }
            ans = max(ans, next - prev);
        }
        return ans;
    }
};
```
## 选项

### A
每日一练社区's avatar
每日一练社区 已提交
93

每日一练社区's avatar
每日一练社区 已提交
94
```cpp
每日一练社区's avatar
每日一练社区 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

class Solution
{
public:
    int longestConsecutive(vector<int> &nums)
    {
        unordered_map<int, int> mp;
        int l = 0, r = 0, res = 0, len = 0;
        for (int num : nums)
        {
            if (!mp[num])
            {
                l = mp[num - 1];
                r = mp[num + 1];
                len = l + r + 1;
                res = max(res, len);
                mp[num] = len;
                mp[num - l] = len;
                mp[num + r] = len;
            }
        }
        return res;
    }
};
```

### B
每日一练社区's avatar
每日一练社区 已提交
122

每日一练社区's avatar
每日一练社区 已提交
123
```cpp
每日一练社区's avatar
每日一练社区 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
class Solution
{

    struct DisJointSet
    {
        vector<int> _id;
        vector<int> _size;
        int max_size;
        int _count;
        DisJointSet(int Num)
        {
            for (int i = 0; i < Num; i++)
            {
                _id.emplace_back(i);
                _size.emplace_back(1);
            }
            _count = Num;
            max_size = 1;
        }

        int find_(int p)
        {
            while (p != _id[p])
            {
                _id[p] = _id[_id[p]];
                p = _id[p];
            }
            return p;
        }

        void _union(int p, int q)
        {
            int i = find_(p);
            int j = find_(q);
            if (i == j)
                return;
            if (_size[i] > _size[j])
            {
                _id[j] = i;
                _size[i] += _size[j];
                max_size = max(max_size, _size[i]);
            }
            else
            {
                _id[i] = j;
                _size[j] += _size[i];
                max_size = max(max_size, _size[j]);
            }
            _count--;
        }
    };

public:
    int longestConsecutive(vector<int> &nums)
    {
        if (nums.size() == 0)
            return 0;
        DisJointSet disJointSet(nums.size());
        unordered_set<int> nums_set;
        unordered_map<int, int> nums_disJointSetID_map;
        for (int i = 0; i < nums.size(); i++)
        {
            if (nums_set.find(nums[i]) != nums_set.end())
                continue;
            nums_set.insert(nums[i]);
            nums_disJointSetID_map[nums[i]] = i;
            if (nums_set.find(nums[i] - 1) != nums_set.end())
            {
                disJointSet._union(nums_disJointSetID_map[nums[i]], nums_disJointSetID_map[nums[i] - 1]);
            }
            if (nums_set.find(nums[i] + 1) != nums_set.end())
            {
                disJointSet._union(nums_disJointSetID_map[nums[i]], nums_disJointSetID_map[nums[i] + 1]);
            }
        }
        return disJointSet.max_size;
    }
};
```

### C
每日一练社区's avatar
每日一练社区 已提交
205

每日一练社区's avatar
每日一练社区 已提交
206
```cpp
每日一练社区's avatar
每日一练社区 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
class Solution
{
public:
    int longestConsecutive(vector<int> &vec)
    {
        set<int> s;
        for (int elem : vec)
            s.insert(elem);

        int longest = 0;
        int cnt = 1;
        for (auto e : s)
        {
            if (s.find(e + 1) != s.end())
            {
                cnt++;
            }
            else
            {
                cnt = 1;
            }
            longest = max(longest, cnt);
        }
        return longest;
    }
};
```