LeetCode hot 100—最长连续序列

题目

给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。

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

示例

示例 1:

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

示例 2:

输入:nums = [0,3,7,2,5,8,4,6,0,1]
输出:9

示例 3:

输入:nums = [1,0,1,2]
输出:3

分析  

枚举每个数x去匹配x+1,x+2,...是否存在会达到O(n^{2}),不符合题意。因此需要注意避免重复枚举,即起点数x一定是在数组中不存在x-1的,因此每次在哈希表中检查是否存在x-1即能判断是否需要跳过。

哈希表法

时间复杂度:O(n)

空间复杂度:O(n)

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        if (nums.empty()) return 0;
        unordered_set<int> numSet(nums.begin(), nums.end());
        int res = 0;
        for (int num : numSet) {
            // 如果 num-1 不在集合中,说明 num 是序列的起点
            if (!numSet.count(num - 1)) {
                int currentNum = num;
                int currentStreak = 1;
                // 向右扩展序列
                while (numSet.count(currentNum + 1)) {
                    currentNum++;
                    currentStreak++;
                }
                // 更新最长连续序列
                res = max(res, currentStreak);
            }
        }
        return res;
    }
};

count 替代 find 效率更高

知识充电

unordered_set是 C++ 标准库中提供的一种容器,用于存储唯一的元素,无序存储。它的底层实现通常基于哈希表,因此查找、插入和删除操作的平均时间复杂度为 O(1),但在最坏情况下可能会退化为 O(n),取决于哈希冲突的数量。

插入元素

insert()

std::unordered_set<int> s; // 创建一个空的 unordered_set
auto result = s.insert(7);
if (result.second) {
    std::cout << "插入成功\n";
} else {
    std::cout << "元素已存在\n";
}

emplace()

s.emplace(50);

查找元素

find()

auto it = s.find(3);
if (it != s.end()) {
    std::cout << "找到了元素: " << *it << std::endl;
} else {
    std::cout << "元素不存在\n";
}

删除元素

erase()

s.erase(4);  // 删除元素 4
auto it = s.find(5);
if (it != s.end()) {
    s.erase(it);  // 使用迭代器删除元素 5
}

clear()

s.clear();  // 清空所有元素

检查键个数

count()

if (s.count(3)) {
    std::cout << "元素 3 存在\n";
} else {
    std::cout << "元素 3 不存在\n";
}

检查容器状态

empty()

if (s.empty()) {
    std::cout << "集合为空\n";
} else {
    std::cout << "集合不为空\n";
}

size()——返回元素数量

遍历元素

for (auto it = s.begin(); it != s.end(); ++it) {
    std::cout << *it << " ";
}
std::cout << std::endl;

// 使用范围基 for 循环
for (const auto& elem : s) {
    std::cout << elem << " ";
}
std::cout << std::endl;

桶操作

bucket_count()——返回 unordered_set 中桶的数量

bucket_size()——返回指定桶中元素数量

bucket()——返回键所在桶的索引

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rigidwill666

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值