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)的复杂度,那么可以先排序。 以下是naive 的先排序的代码实现

 class Solution {
public int longestConsecutive(int[] nums){
Arrays.sort(nums);
if(nums == null || nums.length == 0) return 0;
int result = 1;
int length = 1; for (int i = 1; i < nums.length; i++ ) {
if(nums[i] == nums[i-1] + 1 ){
length ++;
}else if (nums[i] == nums[i-1]){
continue;
}else{
length = 1; }
result = Math.max(result, length);
} return result;
}
}

再思考:

可是本题要求O(n)。
由于序列里的元素是无序的,又要求O(n),想到用哈希set。

代码

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

[leetcode]128. Longest Consecutive Sequence最长连续序列的更多相关文章

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

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

  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] Longest consecutive sequence 最长连续序列

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

  4. [LeetCode] 128. 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.Fo ...

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

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

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

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

  8. Java for LeetCode 128 Longest Consecutive Sequence

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

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

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

随机推荐

  1. maven的环境变量配置

    一: 首先下载maven, 下载地址:http://maven.apache.org/download.html 打开这个连接:选择File下面的apache-maven-3.2.1-bin.zip链 ...

  2. Delphi TMemoryStream写入到字符串和字符串写入到流

    一.TMemoryStream数据写入到字符串里 var lvStream:TMemoryStream; s:AnsiString; p: PAnsiChar; begin lvStream:= TM ...

  3. 尚硅谷springboot学习21-web开发-处理静态资源

    SpringBoot对静态资源的映射规则 @ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFi ...

  4. The value for the useBean class attribute xxx is invalid

    JSP页面报这个错可能的原因: 1:指定的 Bean 类没找到 2:该类不是 public 的,或者找到的 class 文件是 interface 或抽象类 3:Bean 类中没有 public 的无 ...

  5. C语言函数入参压栈顺序为什么是从右向左?

    看到有人提问到,在处理printf/cout时,压栈顺序是什么样的?大家都知道是从右往左,也就是说从右往左的计算,但是,这里的计算不等于输出. a++和++a的压栈的区别:在计算时,遇到a++会记录此 ...

  6. 0 开发的准备工作一一虚拟机virturalbox

    https://www.virtualbox.org/wiki/Linux_Downloads官网下载linux版本 https://www.ubuntu.com/desktop/developers ...

  7. curl命令解析

    curl命令可以实现http post或者get的请求,是linux下的命令行工具 1.1.直接请求url,打印标准输出 1.2.使用-o参数,可以标准输出到指定的位置 [root@VM-3-10-1 ...

  8. 手动安裝TMG2010所需Windows服务和功能

    安装 Forefront TMG 之前,必须运行准备工具,以验证是否已在您的计算机上安装成功安装 Forefront TMG 所需的应用程序.如果在未首先运行准备工具的情况下运行 Forefront ...

  9. trie数的实现

    Trie树又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用字符串 ...

  10. MongoDB模糊查询,以及MongoDB模糊查询时带有括号的情况

    模糊查询 记录如下: { "_id" : ObjectId("5c3d486d24aa9a000526367b"), "name" : &q ...