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 ... 
随机推荐
- 我认为测试应该掌握的SQL语句
			最近在学习Oracle,对测试人员而言必须掌握两种语言:第一种是DML,数据操纵语言 (Data Manipulation Language) 是SQL语言中,负责对数据库对象运行数据访问工作的指令集 ... 
- 实例化(用new的方式)创建一个对象的顺序
			父类静态块--->子类静态块----->父类普通代码块----->父类构造方法------->子类普通代码块----->子类构造方法 如果父类构造方法中调用的非priva ... 
- lightoj1085 线段树+dp
			//Accepted 7552 KB 844 ms //dp[i]=sum(dp[j])+1 j<i && a[j]<a[i] //可以用线段树求所用小于a[i]的dp[j ... 
- ZOJ 1654 - Place the Robots (二分图最大匹配)
			题意:在一个m*n的地图上,有空地,草和墙,其中空地和草能穿透攻击光线,而墙不能.每个机器人能够上下左右攻击,问在地图上最多能放多少个不互相攻击的机器人. 这个题和HDU 1045 - Fire N ... 
- UVa 10328 - Coin Toss (递推)
			题意:给你一个硬币,抛掷n次,问出现连续至少k个正面向上的情况有多少种. 原题中问出现连续至少k个H的情况,很难下手.我们可以试着将问题转化一下. 设dp[i][j]表示抛掷i个硬币出现连续至多j个H ... 
- jstl表达式替换某些字符
			转自:http://www.yiibai.com/jsp/jstl_function_replace.html fn:replace() 函数替换一个字符串与另一个字符串的所有匹配. 语法 fn:re ... 
- adaboost算法
			三 Adaboost 算法 AdaBoost 是一种迭代算法,其核心思想是针对同一个训练集训练不同的分类器,即弱分类器,然后把这些弱分类器集合起来,构造一个更强的最终分类器.(很多博客里说的三个臭皮匠 ... 
- iOS字体设置
			label.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:24]; 字体名如下: Font Family: Amer ... 
- 测试题1 IOS面试基础题
			免责声明:答案来自本人,错误之处敬请谅解 1.用变量a写出以下定义 a.一个整型数 int a=5; b.一个指向整型数的指针 int *a; c.一个指向指针的指针,它指向的指针是指向一个整 ... 
- Configuring Squid as an accelerator/SSL offload for Outlook Web Access
			reference:http://wiki.squid-cache.org/SquidFaq/ReverseProxy/ Configuring Squid as an accelerator/SSL ... 
