leetcode 128. Longest Consecutive Sequence ----- java
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的更多相关文章
- [LeetCode] 128. Longest Consecutive Sequence 求最长连续序列
		
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
 - [LeetCode] 128. Longest Consecutive Sequence 解题思路
		
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
 - Java for LeetCode 128 Longest Consecutive Sequence
		
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
 - [leetcode]128. Longest Consecutive Sequence最长连续序列
		
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
 - Leetcode 128. Longest Consecutive Sequence (union find)
		
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
 - LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
		
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...
 - Leetcode#128 Longest Consecutive Sequence
		
原题地址 1. 把所有元素都塞到集合里2. 遍历所有元素,对于每个元素,如果集合里没有,就算了,如果有的话,就向左向右拓展,找到最长的连续范围,同时在每次找的时候都把找到的删掉.这样做保证了同样的连续 ...
 - 128. Longest Consecutive Sequence(leetcode)
		
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
 - 【LeetCode】128. Longest Consecutive Sequence
		
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
 
随机推荐
- Deep Learning In NLP 神经网络与词向量
			
0. 词向量是什么 自然语言理解的问题要转化为机器学习的问题,第一步肯定是要找一种方法把这些符号数学化. NLP 中最直观,也是到目前为止最常用的词表示方法是 One-hot Representati ...
 - 【NOIP2015】提高组D1 解题报告
			
P1978神奇的幻方 Accepted 描述 幻方是一种很神奇的 N ∗ N 矩阵:它由数字 1,2,3, … … , N ∗ N 构成,且每行.每列及两条对角线上的数字之和都相同. 当 N 为奇数时 ...
 - Android打开新的Activity并同时关闭当前Activity
			
Intent it = new Intent(); it.setClass(EditActivity.this, MainActivity.class); it.setFlags(Intent.FLA ...
 - C++全局变量在多个源代码文件中的使用
			
在比较大的项目中,如果需要使用全局变量,那么就需要注意一些全局变量声明.使用不当引起的问题了. 本篇文章主要内容有两个:普通全局变量.静态全局变量.全局常量. 1.普通全局变量:假设我们需要在多个不同 ...
 - iOS:图片拉伸不变形技巧
			
方法: 假设图片为60*24 CGFloat top = image.height*0.5-1; // 顶端盖高度 CGFloat bottom = top ; // 底端盖高度 CGFloat le ...
 - Gmail新版截图曝光 你还能认得出来吗?
			
Gmail即将迎来巨大的改变.据外媒消息,目前Google正在测试新的网页版Gmail.要知道从Gmail推出以来还从未进行过如此大的改动. 新版Gmail中,界面相比之前,采用了更加扁平话的设计,整 ...
 - 有哪些 PHP 调试技巧?
			
我目前遇到的最让我称赞的debug方式是:xdebug的 xdebug_start_trace(); /* 业务代码 */ xdebug_stop_trace(); 他解决了我长久以来一个代码调试问题 ...
 - HTML5实战教程———开发一个简单漂亮的登录页面
			
最近看过几个基于HTML5开发的移动应用,比如臭名昭著的12036移动客户端就是主要使用HTML5来实现的,虽然还是有点反应迟钝,但已经比较流畅了,相信随着智能手机的配置越来越高性能越来越好,会越来越 ...
 - julia下载QQ.jl
			
julia下载QQ.jl #=""" julia下载QQ.jl 从http://im.qq.com/pcqq/页面中提取出QQ的下载地址,并下载. 2016年4月1日 1 ...
 - HTML--5    JavaScript
			
一.JavaScript简介 1.JavaScript是个什么东西? 它是个脚本语言,需要有宿主文件,它的宿主文件是HTML文件. 2.它与Java什么关系? 没有什么直接的联系,Java是Sun公司 ...