leetcode笔记:Longest Consecutive Sequence -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

    一. 题目描述

    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.

    二. 题目分析

    这道题有两个技巧:

哈希表插入和查找一个数的时间复杂度为O(1);因此,可以使用哈希表来保存数组,以保障对于数组中的元素的查找是常量时间; 一个数与它相邻的数都在同一个连续子序列中,因此,可以在某个数开始进行最长连续子序列判断的时候,可以将与它在同一连续子序列中的元素标记为不作为判断的开始,因为该元素所在的连续子序列处理过了,这样就可以大大较少比较次数,降低计算复杂度;

    由于C++实现中的哈希表是一个无序字典类型,因此,可以将数组元素的值作为关键字,技巧2中每个元素的标记位作为每一个关键字的值,

leetcode笔记:Longest Consecutive Sequence

    对于数组中的每一个元素,先判断其所在连续子序列是否已经处理过了,如果已经处理过了,则直接处理下一个元素;如果还没处理过,则以其为中心,向左向右查找是否有相邻的数存在数组中,如果存在,就将长度加1,并将该元素的标记位置位,表示该元素所在的连续子序列已经处理过了,一直查找下去,直到相邻的数字不存在数组中为止,记录序列的长度,然后处理下一个元素。按照这个方法,在进行最长连续子序列查找的过程中,每个元素只被访问一次,因此计算复杂度为O(n)

    在创建哈希表的过程中,计算复杂度也为O(n),因此,整个算法的时间复杂度为O(n)+O(n)=O(n)

    三. 示例代码

<code class="hljs cpp">class Solution{public:    int longestConsecutive(vector<int>&num)    {        // get the size of the num        int Size = num.size();        // build the hash_table        unordered_map<int, bool="">HashTable;        for (int Index = 0; Index < Size; Index++)        {            int Tmp = num[Index];            HashTable[Tmp] = false;        }        // find the longest consecutive sequence        int LongestNumber = 1;        for (int Index = 0; Index < Size; Index++)        {            int Tmp = num[Index];            if (HashTable[Tmp])            {                continue;            }            int TmpSequence = 1;            while (HashTable.find(++Tmp) != HashTable.end())            {                HashTable[Tmp] = true;                TmpSequence++;            }            Tmp = num[Index];            while (HashTable.find(--Tmp) != HashTable.end())            {                HashTable[Tmp] = true;                TmpSequence++;            }            if (LongestNumber < TmpSequence)            {                LongestNumber = TmpSequence;            }        }        return LongestNumber;    }};</int,></int></code>

    四. 小结

    该题可以在进行一次最大连续子序列查找的过程中将所有在该连续子序列中的元素进行标记,从而减少寻找最长连续子序列的这个过程,降低计算复杂度,使得这个寻找过程的计算复杂度为O(n),

电脑资料

leetcode笔记:Longest Consecutive Sequence》(https://www.unjs.com)。

最新文章