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) + 无序 -> hash table
具体实现应该学会调用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:考虑这样一个聚类模型,在一个数轴上有个无数的聚类,每个聚类由一个或多个数字组成,每个聚类的大小由最大元素减去最小元素得到,每次读入一个元素或者会创造一个新聚类,或者会和其左右的两个聚类合并。这个模型由map来实现,其映射的关系是key->length。具体在c++里用unordered_map来实现。

trap1:会有重复的数字
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:会有重复的数字
            
            // 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;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Given an unsorted array of integers, find the length of t...
    ShutLove阅读 423评论 0 0
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,788评论 0 33
  • 文:ShakespeareSky(莎士比亚斯基)20161217秋明收拾完手上的工作,准备回家时,接到了林梅的电话...
    ShakespeareSky阅读 409评论 0 0
  • 我有一头牛 下山的时候,我牵起牛鼻环 领它跟我走 田在山脚下,牛住远空中 蓝天房前屋后,白云纷纷 春天了,我说 草...
    沐风0715阅读 596评论 2 2
  • 心情不好的时候,坐上一辆陌生的公交车,不用因为怕坐过头而心有所挂。视觉上接收着新鲜地街景,放下所有开心或不开心的。...
    非柘阅读 171评论 0 0