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

For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

Your algorithm should run in O(n) complexity.

就是判断数组中最长的连续数字的长度。

1、直接用排序。。当然不行了,时间复杂度超过了O(n),虽然AC了。

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

2、用set集合。遍历两次,得出结果,由于add,remove,contains都是O(1)的复杂度,所以时间复杂度符合题意。

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

leetcode 128. Longest Consecutive Sequence ----- java的更多相关文章

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

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

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

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

  3. Java for LeetCode 128 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. Y ...

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

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

  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

    原题地址 1. 把所有元素都塞到集合里2. 遍历所有元素,对于每个元素,如果集合里没有,就算了,如果有的话,就向左向右拓展,找到最长的连续范围,同时在每次找的时候都把找到的删掉.这样做保证了同样的连续 ...

  8. 128. Longest Consecutive Sequence(leetcode)

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

  9. 【LeetCode】128. Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

随机推荐

  1. Spark运行环境的安装

    scala-2.9.3:一种编程语言,下载地址:http://www.scala-lang.org/download/    spark-1.4.0:必须是编译好的Spark,如果下载的是Source ...

  2. 如何在windows上搭建ftp服务器

    FTP(File Transfer Protocol)是TCP/IP网络上两台计算机传送文件的协议,使得主机间可以共享文件.目前有很多软件都能实现这一功能,然而windows自带的IIS就可以帮助你搭 ...

  3. java 基本类库包的作用

    tools.jar:工具类库,它跟我们程序中用到的基础类库没有关系. Jre库包含的jar文件(jdk1.6):resources.jar.rt.jar.jsse.jar.jce.jar.charse ...

  4. [转]ps/2键盘线序识别方法

    from: http://www.360doc.com/content/11/0816/19/844619_140875056.shtml 经常看到有人询问ps/2线坏了,更换的时候如何测线序连线,或 ...

  5. SQL Server CONVERT() 函数(转)

    定义和用法 CONVERT() 函数是把日期转换为新数据类型的通用函数. CONVERT() 函数可以用不同的格式显示日期/时间数据. 语法 CONVERT(data_type(length),dat ...

  6. 北大ACM题库习题分类与简介(转载)

    在百度文库上找到的,不知是哪位大牛整理的,真的很不错! zz题 目分类 Posted by fishhead at 2007-01-13 12:44:58.0 -------------------- ...

  7. php生成图片注释

    //生成验证码图片注释 <?php session_start(); $arr = array( 'a','b','c','d','e','f','g','h','i','j','k','l', ...

  8. BZOJ 1568 Blue Mary开公司

    李超线段树. GTMD调了一下午... #include<iostream> #include<cstdio> #include<cstring> #include ...

  9. css3 动画贝塞尔曲线

    http://cubic-bezier.com/#.17,.67,.83,.67 缓动函数速查表: http://www.xuanfengge.com/easeing/easeing/ Ceaser: ...

  10. Making the Grade_滚动数组&&dp

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...