[Leetcode] [Java] Longest consecutive Sequence

Source: Internet
Author: User

Title:

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.

Test Instructions:

Returns the length of the longest contiguous element given an unordered array of integers.

Given [100, 4, 200, 1, 3, 2] ,

The longest sequential sequence of elements is [1, 2, 3, 4]. The length returned is: 4.

The complexity of your algorithm should be O (n).

Algorithm Analysis:

* Sort words to at least O (nlgn) complexity.

* O (n) complexity, currently only found using hash to solve the scheme,

* Add, remove, contains, etc. the complexity of the method is O (1), so the operating complexity of the two traversal is O (n).

* First put the number in a set, get a number, go to the side of the search, get the longest string containing this number,

* and remove the used number from the set (because of a continuous relationship, a number does not appear in two strings). The last comparison is whether the current string is longer than the current maximum string and is updated.

* Continue so until the collection is empty.

* After a element is checked, it should was removed from the set.

* Otherwise, time complexity would be O (MN) in which m are the average length of all consecutive sequences.

AC Code:

<span style= "FONT-SIZE:12PX;" >public static int longestconsecutive (int[] num)     {    //If array is empty, return 0    if (num.length = = 0)     return 0;        set<integer> set = new Hashset<integer> ();    int max = 1;         for (int e:num)    set.add (e);         for (int e:num)     {    int left = e-1;    int right = e + 1;    int count = 1;         while (Set.contains (left))     {    count++;    Set.remove (left);    left--;    }         while (Set.contains (right))     {    count++;    Set.remove (right);    right++;    }         max = Math.max (count, max);    }    return max;}    } </span>


Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source

[Leetcode] [Java] Longest consecutive Sequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.