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. 一个有趣的nginx问题引发的小问题

    最近处理一个nginx问题,故障现象是:所有的work进程,都在等锁.调用的是sem_wait 根据对应的堆栈,查看一下大家等的锁都一样,看看这把锁被谁拿了: 锁的结构是: typedef struc ...

  2. 【364】SVM 通过 sklearn 可视化实现

    先看下效果图: # 先调入需要的模块 import numpy as np import matplotlib.pyplot as plt from sklearn import svm import ...

  3. [CI]CodeIgniter系统流程

    ---------------------------------------------------------------------------------------------------- ...

  4. quartz 实例

    第一步:添加jar包 第二步:在spring配置文件中添加 <context:annotation-config/> 第三步:编写定时代码 我们通常做Java后台接口,是让前端访问的,让前 ...

  5. mysql查找某连续字段中断的编号

    查询dj_pxlb表中zwh 空缺的值 select model2.zwh-1 as kqzwh from (select model1.zwh from dj_pxlb as model1 wher ...

  6. float double 如何存储计算2 (这个写的也不错)

    目前java遵照IEEE制定的浮点数表示法来进行float,double运算.这种结构是一种科学计数法,用符号.指数和尾数来表示,底数定为2——即把一个浮点数表示为尾数乘以2的指数次方再添上符号. 我 ...

  7. mui-当使用addeleventlisener()方法绑定事件时选择器无法绑定事件

    在mui中绑定事件不能用jQuery或mui(“#XX”)的形式选取某个元素,而是document.getelementbyid()的形式 mui(“#XX”)可以使用on方法绑定事件

  8. flume 实际的使用

    1.No configuration found for this host:seqGenSrc bin/flume-ng agent --conf conf --conf-file conf/flu ...

  9. 03_java基础(三)之第一个程序与开发工具的安装

    1.第一个Java程序 ① 找一个特定的放代码的地方    ② 新建一个文本文档 --> 改名 Hello.java        注意 : 必须先把文件的后缀名都显示出来     不同的操作系 ...

  10. mybatis in查询 传入string

    <select id="selectChoosenumberdetailNumber" > SELECT number FROM choosenumberdetail ...