【LeetCode 面试经典150题】128. Longest Consecutive Sequence 最长连续序列

128. Longest Consecutive Sequence

题目大意

Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.

You must write an algorithm that runs in O(n) time.

中文释义

给定一个未排序的整数数组 nums,返回最长连续元素序列的长度。

你必须编写一个在 O(n) 时间内运行的算法。

示例

Example 1:

Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: 最长连续元素序列是 [1, 2, 3, 4]。因此它的长度是 4。

Example 2:

Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9

限制条件

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

解题思路

方法

即在一个未排序的整数数组中找出最长的连续元素序列的长度。为了达到 O(n) 的时间复杂度,使用了哈希集合(unordered_set)来存储数组中的所有元素,这使得查找特定元素的操作变得非常快速。

  1. 创建哈希集合

    • 使用 unordered_set<int> 将数组 nums 中的所有元素添加到集合中。这样可以在 O(1) 时间复杂度内判断一个元素是否存在于集合中。
  2. 遍历数组

    • 遍历数组 nums 中的每个元素 num
      • 首先检查 num - 1 是否存在于集合中,以确定 num 是否是一个连续序列的起始点。
      • 如果 num 是起始点,则从 num 开始向后查找连续的数字,直到找不到下一个连续的数字。
  3. 查找并更新最长序列

    • 对于每个起始点,计算从该点开始的连续序列的长度,并更新最长序列的长度。
  4. 返回结果

    • 最终返回计算得到的最长连续序列的长度。

代码

class Solution {
   
public:
    int longestConsecutive(vector<int>& nums) {
   
        unordered_set<int> num_set(nums.begin(), nums.end());
        int longestStreak = 0;

        for (int num : nums) {
   
            if (!num_set.count(num - 1)) {
   
                int currentNum = num;
                int currentStreak = 1;
                while (num_set.count(currentNum + 1)) {
   
                    currentNum += 1;
                    currentStreak += 1;
                }
                longestStreak = max(longestStreak, currentStreak);
            }
        }
        return longestStreak;
    }
};

相关推荐

  1. leetcode面试经典150】47. 连续序列(C++)

    2024-01-11 14:40:01       30 阅读
  2. LeetCode-热100128.连续序列

    2024-01-11 14:40:01       42 阅读
  3. LeetCode每日刷.09(128.连续序列)

    2024-01-11 14:40:01       52 阅读
  4. LeetCode练习与总结:连续序列--128

    2024-01-11 14:40:01       33 阅读
  5. Leetcode128.连续序列

    2024-01-11 14:40:01       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-11 14:40:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-11 14:40:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-11 14:40:01       87 阅读
  4. Python语言-面向对象

    2024-01-11 14:40:01       96 阅读

热门阅读

  1. 概率生成函数([CTSC2006] 歌唱王国 题解)

    2024-01-11 14:40:01       55 阅读
  2. GO项目自动化-根据库表字段自动生成API

    2024-01-11 14:40:01       52 阅读
  3. 源码编译FFmpeg4.3

    2024-01-11 14:40:01       56 阅读
  4. 【我的Rust库】get_local_info 0.1.7发布

    2024-01-11 14:40:01       47 阅读
  5. Opentsdb官方优化文档 - 翻译

    2024-01-11 14:40:01       42 阅读
  6. Android 车联网——CarOccupantZoneService介绍(十四)

    2024-01-11 14:40:01       53 阅读
  7. resulttype和parametertype的区别

    2024-01-11 14:40:01       56 阅读
  8. Vuex与Vuex-Class的底层原理简单实现

    2024-01-11 14:40:01       56 阅读
  9. leetcode面试经典150题——49 字母异位词分组

    2024-01-11 14:40:01       56 阅读
  10. 【机器学习300问】2、机器学习分为哪几类?

    2024-01-11 14:40:01       45 阅读
  11. 秒杀相关问题及答案(2024)

    2024-01-11 14:40:01       44 阅读
  12. 12.2 队列模拟

    2024-01-11 14:40:01       77 阅读
  13. Linux搭建Kafka详细一步一步指南(linux启动kafka脚本)

    2024-01-11 14:40:01       54 阅读
  14. 鸿蒙OS应用开发之百分比显示组件

    2024-01-11 14:40:01       49 阅读
  15. PostgreSQL字符串分割函数大全

    2024-01-11 14:40:01       45 阅读