码迷,mamicode.com
首页 > 其他好文 > 详细

【Leetcode】Longest Consecutive Sequence

时间:2014-08-20 14:01:12      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:blog   io   for   ar   div   amp   log   size   

 

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;
    }
};

  

【Leetcode】Longest Consecutive Sequence,布布扣,bubuko.com

【Leetcode】Longest Consecutive Sequence

标签:blog   io   for   ar   div   amp   log   size   

原文地址:http://www.cnblogs.com/weixliu/p/3924336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!