Hard-题目8:128. Longest Consecutive Sequence

原创 2016年05月31日 23:26:07

题目原文:
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.
题目大意:
给出一个无序的数组,寻找最长的连续元素,返回其长度。
例如数组[100,4,200,1,3,2]的最长连续元素是[1,2,3,4]所以返回4.
要求在线性时间复杂度内解决。
题目分析:
要求没做到,用了朴素的办法水过去的。复杂度O(nlogn).,先排序,再扫描一遍数组寻找最大的连续元素。如果遇到重复元素则跳过。
源码:(language:java)

public class Solution {
    public int longestConsecutive(int[] nums) {
        Arrays.sort(nums);
        int longest = 1;
        int max = 1;
        for(int i = 1;i<nums.length;i++) {
            if(nums[i]==nums[i-1])
                continue;
            else if(nums[i]-nums[i-1]==1) {
                longest++;
                if(longest>max)
                    max = longest;
            }
            else
                longest=1;
        }
        return max;
    }
}

成绩:
5ms,beats 95.00%,众数16ms,11.95%
Cmershen的碎碎念:
本题的tags给的是并查集,我想到的大致思路是,把所有相邻的都加入一个集合中,并维护每个集合的最大值和最小值,遍历数组的每个元素n,如果n-1或n+1都在集合中,则把这两个集合并起来,如果只有一个在,则加入相应集合,如果都不在则自成一个集合。但还没实现,而且也不确定是否是O(n)时间的。而O(nlogn)的复杂度下的完成时间确是很快的(似乎小于线性算法),可能是用例的数量不够大。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

LeetCode 128. Longest Consecutive Sequence

题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements seq...

leetcode_question_128 Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. ...
  • doc_sgl
  • doc_sgl
  • 2013年09月20日 14:24
  • 1270

【LeetCode】128. Longest Consecutive Sequence

128. Longest Consecutive Sequence

LeetCode 128 Longest Consecutive Sequence

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

Longest Consecutive Sequence - LeetCode 128

题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements sequ...
  • bu_min
  • bu_min
  • 2015年05月27日 08:22
  • 250

154. Find Minimum in Rotated Sorted Array II \ 128. Longest Consecutive Sequence

Find Minimum in Rotated Sorted Array II 题目解析 代码实现154. Find Minimum in Rotated Sorted Array II题目解析Sup...

LeetCode Algorithms 128. Longest Consecutive Sequence 题解

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

[LeetCode]128.Longest Consecutive Sequence

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

Leetcode_128_Longest Consecutive Sequence

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43854597 Given an unsorted...

LeetCode: Longest Consecutive Sequence [128]

【题目】 Given an unsorted array of integers, find the length of the longest consecutive elements seque...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Hard-题目8:128. Longest Consecutive Sequence
举报原因:
原因补充:

(最多只允许输入30个字)