题目:

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

Your algorithm should run in O(n) complexity.

Example:

Input: [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.

分析:

给定一个未排序的整数数组,找出最长连续序列的长度。

可以先对数组进行排序,然后遍历数组,判断数字是否连续来计算最大长度,不过由于排序,时间复杂度是O(nlogn),我们可以利用哈希表来存储数组元素,再遍历元素,当前元素为num时,如果num-1这个元素不在我们的集合中,就代表这个num可以作为序列的起始点,然后依次判断num++是否在集合中,更新当前序列最大长度,当出现num++不在集合中,也就是此时序列不再连续,更新全局最大长度,继续遍历数组,最后返回全局的最大长度即可。

程序:

C++

class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> set(nums.begin(), nums.end());
int res = 0;
for(int num:nums){
if(!set.count(num-1)){
int l = 1;
while(set.count(++num)){
l++;
}
res = max(res, l);
}
}
return res;
}
};

Java

class Solution {
public int longestConsecutive(int[] nums) {
if(nums.length == 0)
return 0;
int res = 0;
Set<Integer> set = new HashSet<>();
for(int num:nums)
set.add(num);
for(int num:nums){
if(!set.contains(num-1)){
int l = 1;
while(set.contains(++num))
l++;
res = Math.max(l, res);
}
}
return res;
}
}

LeetCode 128. Longest Consecutive Sequence 最长连续序列 (C++/Java)的更多相关文章

  1. [leetcode]128. Longest Consecutive Sequence最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...

  2. 128. Longest Consecutive Sequence最长连续序列

    [抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

  3. 298. Binary Tree Longest Consecutive Sequence最长连续序列

    [抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...

  4. [Leetcode] Longest consecutive sequence 最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  5. [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  6. LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...

  7. [LeetCode] 128. Longest Consecutive Sequence 解题思路

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  8. leetcode 128. Longest Consecutive Sequence ----- java

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. Java for LeetCode 128 Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  10. Leetcode 128. Longest Consecutive Sequence (union find)

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...

随机推荐

  1. C#的窗体假关闭操作例子 - 开源研究系列文章

    晚上编码的时候,想到了以前编写的窗体关闭的事情,就是带托盘图标的应用,有一个主显示操作窗体,但是主窗体点击关闭按钮的时候,实际上是窗体隐藏而非真正关闭,这个在其它的一些应用程序里有这个效果.于是就想到 ...

  2. 痞子衡嵌入式:在i.MXRT1xxx系列上用NAND型启动设备时可用两级设计缩短启动时间

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是在i.MXRT1xxx系列上用NAND型启动设备时可用两级设计缩短启动时间. 去年痞子衡写过一篇骚操作文章 <借助i.MXRT10 ...

  3. 为什么序列化要实现Serializable接口

    为什么实现了Serializable接口就会序列化? 实现了Java中的Serializable接口的类会被称为可序列化的,这意味着它们的实例可以被序列化为字节流,以便于在网络上传输.保存到文件中或者 ...

  4. 力扣33(java&python)-搜索旋转排序数组(中等)

    题目: 整数数组 nums 按升序排列,数组中的值 互不相同 . 在传递给函数之前,nums 在预先未知的某个下标 k(0 <= k < nums.length)上进行了 旋转,使数组变为 ...

  5. 力扣69(java&python)-x的平方根(简单)

    题目: 给你一个非负整数 x ,计算并返回 x 的 算术平方根 . 由于返回类型是整数,结果只保留 整数部分 ,小数部分将被 舍去 . 注意:不允许使用任何内置指数函数和算符,例如 pow(x, 0. ...

  6. 首次公开!阿里云开源PolarDB总体架构和企业级特性

    ​简介:在3月2日的阿里云开源 PolarDB 企业级架构发布会上,阿里云 PolarDB 内核技术专家北侠带来了主题为<PolarDB 总体架构设计和企业级特性>的精彩演讲. 在3月2日 ...

  7. KubeMeet|聊聊新锐开源项目与云原生新的价值聚焦点

    ​简介: 10 月 16 日上海,OAM/KubeVela.OpenKruise.OCM 三大开源项目的社区负责人.核心贡献者和企业用户将齐聚 KubeMeet,和现场 100 名开发者聊聊新的技术环 ...

  8. Codeforces Round 917 (Div. 2)

    A. Least Product 存在 \(a[i] = 0\),\(min = 0\),不需要任何操作. 负数个数为偶数(包括0),\(min = 0\),把任意一个改为 \(0\). 负数个数为奇 ...

  9. 教你在windows10系统中安装python3(下载安装+配置教程)

    官网地址: https://www.python.org/ 参考文档: https://www.chhui.cn/post-1039.html

  10. golang url解析

    package main import "fmt" import "net/url" import "strings" func main( ...