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. 批量杀死多个进程 linux kill

    批量杀进程 -| “grep -v grep”是在列出的进程中去除含有关键字“grep”的进程. “cut -c 9-15”是截取输入行的第9个字符到第15个字符,而这正好是进程号PID,也有使用aw ...

  2. Servlet基本_画面遷移

    画面遷移方法は.下記ようがある.・リクエストのディスパッチ・リダイレクト(画面から) 1.ディスパッチ1)概念サーブレットから他のリソース(サーブレット.JSP.Htmlなど)にリクエストを転送するこ ...

  3. creator.d.ts 的错误

    //export class PhysicsCollider{ export class PhysicsCollider extends Collider{ ==================检查代 ...

  4. Delphi在Listview中加入Edit控件

    原帖 : http://www.cnblogs.com/hssbsw/archive/2012/06/03/2533092.html Listview是一个非常有用的控件,我们常常将大量的数据(如数据 ...

  5. scala private

    class Person private(val name:String) private 修饰整个类的参数,其实效果类似于java的私有化构造方法,无法通过new Person(..) 来实例化对象 ...

  6. 限制ssh登录ip和系统用户

    一般对于安全性要求比较高的系统,限制ssh登录的源ip地址和可以登录的系统账户是非常有必要的,ssh登录的源地址和可以登录的系统账户的设置在sshd的配置文件/etc/ssh/sshd_config中 ...

  7. Swift中的的注释

    1. Swift支持与OC中相同的     /**/  ./***/  不同点Swift支持 /*/**/ 这样的注释  ,多行注释 2. 直接双杠注释 // 单行注释 3. 利用 //MARK: 返 ...

  8. EF 数据查询效率对比

    优化的地方: 原地址:https://www.cnblogs.com/yaopengfei/p/9226328.html ①:如果仅是查询数据,并不对数据进行增.删.改操作,查询数据的时候可以取消状态 ...

  9. php图片转base64

    /*读取问价家图片生澈哥哥js文件 */header("Access-Control-Allow-Origin: *");$i=0;$handle = opendir('./ima ...

  10. python全栈开发 随笔 'is' 和 == 的比较知识与区别 编码和解码的内容及转换

    python 一. is 和 == 的区别; == 比较的是两边的值. a = 'alex' b = 'alex' print(a = b) #True a = 10 b = 10 print(a = ...