Leetcode: Longest Consecutive Sequence, set

Source: Internet
Author: User

Leetcode: Longest Consecutive Sequence, set

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.

First sort and then scan once, maintain a maximum consecutive sequence length this method is very direct, but at least need O (NlongN). Don't let the sorting, it is necessary to think, it is estimated that we have to use space in exchange for time.

Place the number in a collection, get a number, and search for the number on both sides to see if the number is in the collection. In this way, the maximum length of the string containing the number is obtained, and remove the used numbers from the set (because of the continuous relationship, a number will not appear in two strings ). Finally, compare whether the current string is longer than the current maximum string. If yes, update the current string. Continue until the set is empty. If we use HashSet to store numbers, we can think that the access time is constant, then the algorithm needs to scan once to create a set, and the second scan to find the longest string, therefore, the complexity is O (2 * n) = O (n), and the space complexity is the size of the set, that is, O (n ).

This question also uses Iterator: 10-11 rows

 1 public class Solution { 2     public int longestConsecutive(int[] num) { 3         if (num==null || num.length==0) return 0; 4         HashSet<Integer> set = new HashSet<Integer>(); 5         for (int elem : num) { 6             set.add(elem); 7         } 8         int longest = 1; 9         while (!set.isEmpty()) {10             Iterator iter = set.iterator();11             int item = (int)iter.next();12             set.remove(item);13             int len = 1;14             int i = item - 1;15             while (set.contains(i)) {16                 len++;17                 set.remove(i--);18             }19             i = item + 1;20             while (set.contains(i)) {21                 len++;22                 set.remove(i++);23             }24             if (len > longest) longest = len;25         }26         return longest;27     }28 }

 




Related Article

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.