title: Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example, Given [4, 1, 3, 2], the longest consecutive elements sequence are [1, 2, 3, 4]. Return its length:4.
Your algorithm should run in O (n) complexity.
Analysis: Given an array of integers with no ordering, the length of the longest consecutive number is found, requiring the complexity to be O (n). This problem is not very difficult, I directly first sort and then find. Give three kinds of solution, the first solution is my own design, the latter two kinds of reference on the Internet is a better solution to learn.
Code:
<span style= "FONT-SIZE:14PX;" > #include "stdafx.h" #include <vector> #include <unordered_map> #include <algorithm> #include < Iostream>using namespace Std;class solution{public://time O (n) int longstconsecutivesequence (vector<int> & num) {//sorted sort (Num.begin (), Num.end (),less<int> ()); int max = 1;//record longest consecutive number int n = 1;//Record single consecutive number of BOOL flag = FALSE; Identifies whether continuous int temp =-1; Record the previous element vector<int>::const_iterator cit;for (cit = Num.begin (); CIT! = num.end (); cit++) {if (flag) {if (*cit = = ( Temp + 1)) {n++;} Else{flag = False;if (n>max) {max = n;} n = 1;}} Else{flag = true;if (*cit = = (temp + 1)) {n++;}} temp = *cit;} Possibly the last consecutive maximum if (N>max) {max = n;} return Max;} int LongstConsecutiveSequence2 (vector<int> &num) {unordered_map<int, bool> used;vector<int>:: Iterator it; Initialize, assign a value to map for (it = Num.begin (); it = Num.end (); it++) Used[*it] = False;int longest=0;for (it = Num.begin (); it! = NUM.E nd (); it++) {if (Used[*it]) continue;int length= 1;used[*it] = true;//look both ways for (int j = *it + 1; Used.find (j)! = Used.end (); ++j) {used[j] = true;++length;} for (int j = *it-1; Used.find (j)! = Used.end ();--j) {used[j] = true;++length;} Take the maximum longest = max (longest, length);} return longest;} int LongstConsecutiveSequence3 (vector<int> &num) {unordered_map<int, int> map;int size = Num.size (); int L = 1;for (int i = 0; i < size; i++) {if (Map.find (num[i])! = Map.end ()) continue;map[num[i]] = 1;if (Map.find (num[ I]-1)! = Map.end ()) {L = max (L, Mergecluster (map, Num[i]-1, num[i]));} if (Map.find (Num[i] + 1)! = Map.end ()) {L = max (L, Mergecluster (map, Num[i], Num[i] + 1));}} return size = = 0? 0:l;} Private:int Mergecluster (Unordered_map<int, int> &map, int left, int. right) {int upper = right + Map[right]-1; int lower = Left-map[left] + 1;int length = Upper-lower + 1;map[upper] = length;map[lower] = Length;return length;}}; int _tmain (int argc, _tchar* argv[]) {vector<int> a;a.push_back (5); A.push_back (6); A.push_back (1); A.push_back (2); A.push_back (3); A.push_back (+); A.push_back (A.push_back); A.push_back (25 ); A.push_back (7); A.push_back (100); Solution S;int RTN = S.longstconsecutivesequence2 (a) cout << rtn << endl;system ("pause"); return 0;} </span>personal Feeling the second kind is better, easy to understand.
Today brush two Leetcode, took a good long time, the moment feel the qualification is too mediocre .....
Leetcode's longst consecutive Sequence