leetcode 128 Longest Consecutive Sequence

Question:
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.

int longestConsecutive(int* nums, int numsSize) {
    
}

解法1: O(n) + 無(wú)序 -> hash table
具體實(shí)現(xiàn)應(yīng)該學(xué)會(huì)調(diào)用unordered_map

functions
find()
end()
//
//  main.cpp
//  leetcode
//
//  Created by YangKi on 15/11/09.
//  Copyright ? 2015年 YangKi. All rights reserved.
//

#include<cstdlib>
#include<iostream>
#include<vector>
#include<unordered_map>//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
using namespace std;


class Solution
{
public:
    int longestConsecutive(vector<int>& nums)
    {
        if( nums.empty()==true ) return 0;
        
        unordered_map<int, bool> used;
        for ( int i: nums ) used[i] = false;
        int longest = 1;
        
        for(int i: nums)
        {
            if (used[i] == true) continue;
            int length=1;
            used[i]=true;
            
            for(int j=i+1; used.find(j)!=used.end(); j++)
            {
                length++;
                used[j]=true;
            }
            
            for(int j=i-1; used.find(j)!=used.end(); j--)
            {
                length++;
                used[j]=true;
            }
            
            
            if(length>longest) longest=length;
        }
        
        return longest;
    }
};

int main()
{
    Solution *s=new Solution();
    vector<int>v={};
    printf("%d\n", s->longestConsecutive(v));
    return 0;
}


解法2:考慮這樣一個(gè)聚類模型,在一個(gè)數(shù)軸上有個(gè)無(wú)數(shù)的聚類,每個(gè)聚類由一個(gè)或多個(gè)數(shù)字組成,每個(gè)聚類的大小由最大元素減去最小元素得到,每次讀入一個(gè)元素或者會(huì)創(chuàng)造一個(gè)新聚類,或者會(huì)和其左右的兩個(gè)聚類合并。這個(gè)模型由map來(lái)實(shí)現(xiàn),其映射的關(guān)系是key->length。具體在c++里用unordered_map來(lái)實(shí)現(xiàn)。

trap1:會(huì)有重復(fù)的數(shù)字
trap2:only modify the length of low and high

//
//  main.cpp
//  leetcode
//
//  Created by YangKi on 15/11/10.
//  Copyright ? 2015年 YangKi. All rights reserved.
//

#include<cstdlib>
#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;


class Solution
{
public:
    int longestConsecutive(vector<int>& nums)
    {
        int size = nums.size();
        int l=1;
        unordered_map<int, int>map;
        
        for (int i=0; i<size ; i++)
        {
            if(map.find(nums[i])!= map.end()) continue; //trap1:會(huì)有重復(fù)的數(shù)字
            
            // map does not contain nums[size] yet
            map[ nums[i] ]=1;//at least a one element cluster
            
            //have to merge?
            if(map.find( nums[i]-1 )!=map.end())//can merge the left part
            {
                l = max( clusterMerge(map, nums[i]-1, nums[i]), l);
            }
            
            if(map.find( nums[i]+1 )!=map.end())//can merge the right part
            {
                l = max( clusterMerge(map, nums[i], nums[i]+1), l);
            }
        }
        
        return size == 0? 0: l;
    }
    
private:
    int clusterMerge(unordered_map<int , int> &map, int left, int right)// attention:!!!!!!use &
    {
        int low=left-map[left]+1;
        int high=right+map[right]-1;
        int length = high-low+1;
        map[low]=length;//trap2:only modify the length of low and high
        map[high]=length;
        return length;
    }
};

int main()
{
    Solution *s=new Solution();
    vector<int>v={3,2,4,1};
    printf("%d\n", s->longestConsecutive(v));
    return 0;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Given an unsorted array of integers, find the length of t...
    ShutLove閱讀 423評(píng)論 0 0
  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,769評(píng)論 0 33
  • 文:ShakespeareSky(莎士比亞斯基)20161217秋明收拾完手上的工作,準(zhǔn)備回家時(shí),接到了林梅的電話...
    ShakespeareSky閱讀 407評(píng)論 0 0
  • 我有一頭牛 下山的時(shí)候,我牽起牛鼻環(huán) 領(lǐng)它跟我走 田在山腳下,牛住遠(yuǎn)空中 藍(lán)天房前屋后,白云紛紛 春天了,我說(shuō) 草...
    沐風(fēng)0715閱讀 582評(píng)論 2 2
  • 心情不好的時(shí)候,坐上一輛陌生的公交車,不用因?yàn)榕伦^(guò)頭而心有所掛。視覺(jué)上接收著新鮮地街景,放下所有開(kāi)心或不開(kāi)心的。...
    非柘閱讀 170評(píng)論 0 0