Level:

  Hard

题目描述:

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.

思路分析:

  设置一个hashset,保存数组中出现的值,然后遍历hashset,每访问到一个值num,我们查看num-1,和num+1是否在集合中,如果存在那我们可以继续查找num-2,和num+2是否存在,我们按照这种方法查找,直到要查找的数不存在,可以计算出一个连续的序列长度,对集合中每个元素进行上述操作,最后得出一个最长的连续序列。

代码:

public class Solution{
public int longestConsecutive(int []nums){
if(nums==null||nums.length==0)
return 0;
HashSet<Integer>set=new HashSet<>();
int res=0; //记录结果
for(int n:nums)
set.add(n);
while(!set.isEmpty()){
int num=getfirst(set);
set.remove(num);
int left=0;//记录当前num向左能延伸的距离
while(set.contains(num-1)){
left++;
set.remove(num-1);
num--;
}
int right=0; //记录当前num向右能延伸的距离
num=num+left;
while(set.contains(num+1)){
right++;
set.remove(num+1);
num++;
}
res=Math.max(res,left+right+1);
}
return res;
}
public int getfirst(HashSet<Integer>set){//返回集合中第一个元素
for(int num:set)
return num;
return 0;
}
}

76.Longest Consecutive Sequence(最长的连续序列)的更多相关文章

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

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

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

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

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

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

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

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

  5. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

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

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

  7. [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  8. [Swift]LeetCode298. 二叉树最长连续序列 $ Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

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

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

随机推荐

  1. Linux学习之旅(一)Linux常用命令

    pwd命令  显示当前所在的目录 ls命令  显示目录下的子目录和文件 ls 显示当前目录下的子目录和文件 ls -a 显示当前目录下的所以子目录和文件(包括隐藏文件和文件夹) ls -al    显 ...

  2. Oracle 附加日志(supplemental log)

    参考资料: 1.https://blog.csdn.net/li19236/article/details/41621179

  3. uboot移植之迷雾解码

    按照蜗窝科技的步骤执行 一.有关硬件描述的填空题 1)CPU上电后,从哪种设备(       BOOTROM         )的哪个地址(        0x0000_0000       )开始执 ...

  4. POJ 2018 Best Cow Fences (二分答案构造新权值 or 斜率优化)

    $ POJ~2018~Best~Cow~ Fences $(二分答案构造新权值) $ solution: $ 题目大意: 给定正整数数列 $ A $ ,求一个平均数最大的长度不小于 $ L $ 的子段 ...

  5. PDO扩展

    <?php class db extends PDO { private $error; private $sql; private $bind; private $errorCallbackF ...

  6. 《Tomcat权威指南》读书笔记

    第一章 Tomcat的开幕式 1.Tomcat是以Java编写的,这表示在能够构建和测试它之前,必须安装最新的.完整的JAVA运行环境(JRE,Java runtime). 2.Catalina To ...

  7. MAN VGEXTEND

    VGEXTEND(8)                                                        VGEXTEND(8) NAME/名称       vgexten ...

  8. Jenkins 使用python进行调度,并下载apphost上的安装包

    在持续集成的过程中,Jenkins工具是我们必须要会用的工具,那么今天分享一个使用python对Jenkins进行调度的案例 使用的是python-jenkins 库,借用selenium登陆jenk ...

  9. 【leetcode】1027. Longest Arithmetic Sequence

    题目如下: Given an array A of integers, return the length of the longest arithmetic subsequence in A. Re ...

  10. 小A与最大子段和 斜率优化 + 二分 + 细节

    Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...