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. 用ant进行编译和打包(java)

    ant是目前java环境下最好用的打包部署工具,其采用xml的格式进行编写,功能非常强大.现介绍一下如何手工使用ant进行java程序的编译打包.一.安装ant1.下载并安装ant.到官方主页http ...

  2. win7 MS SQL SERVER 2000安装

    http://blog.chinaunix.net/uid-24398518-id-2156226.html MicrosoftInternetExplorer402DocumentNotSpecif ...

  3. 【STL】- vector的用法

    初始化: 1. 默认构造: vector<int> vint; 2. 用包含10个元素的数组初始化: vector<int> vint(ia, ia+10); 算法: 1. v ...

  4. Rhel6-heartbeat配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119 server19.example.com 192.168.12 ...

  5. hdu 1033 (bit masking, utilization of switch, '\0' as end of c string) 分类: hdoj 2015-06-15 21:47 37人阅读 评论(0) 收藏

    bit masking is very common on the lower level code. #include <cstdio> #include <algorithm&g ...

  6. 解决json跨域时错误:SyntaxError: invalid label

    将数据做以下返回: $callback = $_GET['callback']; echo $callback.'('.json_encode(array('html'=>$html)).')' ...

  7. Gmail新版截图曝光 你还能认得出来吗?

    Gmail即将迎来巨大的改变.据外媒消息,目前Google正在测试新的网页版Gmail.要知道从Gmail推出以来还从未进行过如此大的改动. 新版Gmail中,界面相比之前,采用了更加扁平话的设计,整 ...

  8. 记录一些容易忘记的属性 -- UILabel

    一:UILabel lbl.alpha=0.f;    lbl 透明,会影响子视图的显示 lbl.backgroundColor=[UIColor clearColor]; lbl 背景色透明,子视图 ...

  9. sql server日志不能shrink或truncate

    Backup log [dbxxx] with truncate_only sql server 2008之后不支持此操作,需要改为: BACKUP LOG dbxxx TO DISK='NUL:' ...

  10. Oracle数据库DECODE函数的使用.

    decode函数是Oracle数据库独有的. 语法为: decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 例子:select decode(sign(变量1-变量2) ...