"Leetcode" longest consecutive Sequence problem solving report

Source: Internet
Author: User

Topic

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

for example,
given [100, 4, 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

Given an array without sorting, find the longest contiguous subsequence and return its length.

For example: given [100, 4, 200, 1, 3, 2], its longest contiguous subsequence is [1, 2, 3, 4], so returns its length 4.

The complexity of the algorithm needs to be O (n).


"Method One"

With a hashset, first put all the elements in, and then walk through each element, to see whether the number of adjacent to the left side of the element is also in the HashSet, if you delete it from the hashset, continue to the left, and the right.

public class Solution {public    int longestconsecutive (int[] num) {        if (num = = NULL | | Num.length < 1) return 0;                set<integer> Set = new hashset<integer> ();        for (int x:num) {            set.add (x);        }                int ans = 1;        for (int x:num) {            int left = x-1;            int right = x + 1;            int count = 1;                        while (Set.contains (left)) {                set.remove (left);                left--;                count++;            }                        while (Set.contains (right)) {                set.remove (right);                right++;                count++;            }                        Ans = math.max (count, ans);        }                return ans;    }}

to son: http://www.programcreek.com/2013/01/leetcode-longest-consecutive-sequence-java/


"Method Two"

Use a hashmap to store the length of the contiguous sequence in which element x is located, traverse each element x in num, and see if x-1 and x+1 already exist with a sequential sequence, and if so, X connects the two sequences, and x corresponds to the length of these two sequences and adds 1. The sequence length corresponding to each element in the new sequence is also updated. If element x already exists in HashMap, it means that the sequence in which X is located has been accessed and skipped directly.

public class Solution {public    int longestconsecutive (int[] num) {        int ans = 1;        Store Num[i] ' s consecutive sequence length.        Hashmap<integer, integer> map = new Hashmap<integer, integer> ();        for (int x:num) {            if (!map.containskey (x)) {                int left = Map.containskey (x-1)? Map.get (X-1): 0;                int right = Map.containskey (x + 1)? Map.get (x + 1): 0;                int sum = left + right + 1;                Map.put (x, sum);                                Ans = math.max (ans, sum);                                Map.put (X-left, sum);                Map.put (x + right, sum);            }        }        return ans;    }}

From: https://oj.leetcode.com/submissions/detail/17876074/


"Leetcode" longest consecutive Sequence problem solving report

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.