Leetcode | | 128, longest consecutive Sequence

Source: Internet
Author: User

Problem:

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.

Hide TagsArrayTest instructions: Finding the longest contiguous subsequence in a random ordinal group (no order required)

Thinking:

(1) requires time complexity of O (n), can only use Hashtable.

(2) Unordered_map bottom is realized with Hashtable, can take as Hashtable use

Code

Class Solution {Public:int longestconsecutive (vector<int> &num) {unordered_map<int,int> Hashta        ble            for (int i = 0;i < Num.size (); i++) {if (Hashtable.find (num[i])! = Hashtable.end ()) continue;            int minus_1 = num[i]-1;            int plus_1 = num[i] + 1;            Unordered_map<int,int>::iterator Minus_1iter,plus_1iter;            Minus_1iter = Hashtable.find (minus_1);            Plus_1iter = Hashtable.find (plus_1); if (Minus_1iter! = Hashtable.end () && plus_1iter! = Hashtable.end ()) {Hashtable[num[i]] = Hashtable                [Minus_1] + hashtable[plus_1] + 1;                Hashtable[num[i]-hashtable[minus_1]] = hashtable[num[i]];            Hashtable[num[i] + hashtable[plus_1]] = hashtable[num[i]]; } else if (Minus_1iter = = Hashtable.end () && Plus_1iter = = Hashtable.end ()) {Hashtable[n            Um[i]] = 1; } else if (minus_1iter! = Hashtable.end ()) {Hashtable[num[i]] = Hashtable[minus_1] + 1;            Hashtable[num[i]-hashtable[minus_1]] = hashtable[num[i]];                } else {Hashtable[num[i]] = Hashtable[plus_1] + 1;            Hashtable[num[i] + hashtable[plus_1]] = hashtable[num[i]];        }}//find the maxlen int ans = int_min; for (Unordered_map<int,int>::iterator iter = Hashtable.begin (); ITER! = Hashtable.end (); ++iter) {if (iter-&        Gt;second > ans) ans = iter->second;    } return ans; }};


Leetcode | | 128, 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.