首页 > 代码库 > 【Leetcode】Longest Consecutive Sequence

【Leetcode】Longest Consecutive Sequence

 

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. 

 

这里想要达到线性,必定需要借助hash表,所以思路就是利用hash进行扩展,记录length的长度即可~
 
1st (2 tries)
class Solution{public:    int longestConsecutive(vector<int> &num)     {        // Note: The Solution object is instantiated only once and is reused by each test case.        map<int,int> hashtable;        for(int i = 0;i < num.size();i++)        {            hashtable[num[i]] = i;        }                int count = 0;        int max = 0;        for(int i = 0;i < num.size();i++)        {            count = 1;            int curincrease = num[i] + 1;            while( hashtable.find(curincrease) != hashtable.end() )            {                count++;                hashtable.erase(hashtable.find(curincrease));                curincrease++;            }            int curdecrease = num[i] - 1;            while( hashtable.find(curdecrease) != hashtable.end() )            {                count++;                hashtable.erase(hashtable.find(curdecrease));                curdecrease--;            }            if(count > max)                max = count;        }        return max;    }};

  

2nd (2tries)
class Solution {public:    int longestConsecutive(vector<int> &num) {        //why I don‘t have no idea for this problem???O(n) imply that you must use hashtable???        unordered_map<int,int> hashtable;        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[num[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->second > ans)                ans = iter->second;        }        return ans;    }};