qidao123.com技术社区-IT企服评测·应用市场

标题: LeetCode Hot100【哈希-128.最长连续序列】 [打印本页]

作者: 涛声依旧在    时间: 2025-5-6 11:42
标题: LeetCode Hot100【哈希-128.最长连续序列】
标题:128. 最长连续序列

  1. class Solution {
  2. public:
  3.     int longestConsecutive(vector<int>& nums) {
  4.         unordered_set<int> num_set;  // 使用哈希集合存储数组元素
  5.         for (const int& num : nums) {
  6.             num_set.insert(num);  // 插入数组中的每个数
  7.         }
  8.         int longestStreak = 0;  // 记录最长连续序列的长度
  9.         for (const int& num : num_set) {
  10.             // 如果 num - 1 不在集合中,则 num 是序列的起点
  11.             if (!num_set.count(num - 1))
  12. {
  13.                 int currentNum = num;
  14.                 int currentStreak = 1;
  15.                 // 检查是否存在 num + 1, num + 2, ..., 形成连续序列
  16.                 while (num_set.count(currentNum + 1)) {
  17.                     currentNum += 1;
  18.                     currentStreak += 1;
  19.                 }
  20.                 // 记录最大长度
  21.                 longestStreak = max(longestStreak, currentStreak);
  22.             }
  23.         }
  24.         return longestStreak;
  25.     }
  26. };
复制代码
实行流程

  1. nums = {100, 4, 200, 1, 3, 2}
  2. ;
复制代码
实行步调如下:

步调 1:构建 unordered_set

哈希集合 num_set 存储:
  1. {100, 4, 200, 1, 3, 2}
复制代码
步调 2:遍历 num_set 并计算最长连续序列

遍历元素


时间 & 空间复杂度

操作时间复杂度空间复杂度插入 unordered_setO(n)O(n)遍历 unordered_setO(n)O(1)查询 unordered_set.count()O(1)O(1) 最终

关键点解析

优化 & 变种

假如 nums 颠末 排序,可以利用 O(n log n) 的解法:
  1. int longestConsecutive(vector<int>& nums) {
  2.     if (nums.empty()) return 0;
  3.     sort(nums.begin(), nums.end());
  4.     int longest = 1, current = 1;
  5.     for (int i = 1; i < nums.size(); i++) {
  6.         if (nums[i] == nums[i - 1]) continue;  // 跳过重复项
  7.         if (nums[i] == nums[i - 1] + 1) {
  8.             current += 1;
  9.         } else {
  10.             longest = max(longest, current);
  11.             current = 1;
  12.         }
  13.     }
  14.     return max(longest, current);
  15. }
复制代码
但 哈希表解法更优,因为 无需排序,时间复杂度更低(O(n))。

基础知识

1. unordered_set<int>

  1. unordered_set<int> num_set;
复制代码

基本操作
  1. unordered_set<int> s;
  2. s.insert(10);
  3. s.count(10); // 返回 1,表示存在
  4. s.count(5);  // 返回 0,表示不存在
复制代码
2. for (const int& num : num_set)

  1. for (const int& num : num_set) { ... }
复制代码

3. num_set.count(num - 1)

  1. if (!num_set.count(num - 1))
复制代码

4. while (num_set.count(currentNum + 1))

  1. while (num_set.count(currentNum + 1)) {
  2.     currentNum += 1;
  3.     currentStreak += 1;
  4. }
复制代码

示例
  1. unordered_set<int> s = {1, 2, 3, 4};
  2. int num = 1, streak = 1;
  3. while (s.count(num + 1)) {
  4.     num += 1;   // num 变成 2, 3, 4
  5.     streak += 1; // streak 变成 2, 3, 4
  6. }
复制代码
最终 streak = 4。
5. max(longestStreak, currentStreak)

  1. longestStreak = max(longestStreak, currentStreak);
复制代码

总结

C++语法作用unordered_set哈希集合(去重 & 快速查询)for (const int& num : num_set)遍历哈希表count(x)查抄集合是否包罗 xwhile (num_set.count(currentNum + 1))查找连续元素max(a, b)取最大值
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/) Powered by Discuz! X3.4