LeetCode_Longest Consecutive Sequence

一.题目

Longest Consecutive Sequence

Total Accepted: 33824 Total
Submissions: 116565My
Submissions

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.

Show Tags

Have you met this question in a real interview?

Yes

No

Discuss

二.解题技巧

这道题有两个技巧:

1.哈希表插入和查找一个数的时间复杂度为O(1);因此,可以使用哈希表来保存数组,以保障对于数组中的元素的查找是常量时间;

2.一个数与它相邻的数都在同一个连续子序列中,因此,可以在某个数开始进行最长连续子序列判断的时候,可以将与它在同一连续子序列中的元素标记为不作为判断的开始,因为该元素所在的连续子序列处理过了,这样就可以大大较少比较次数,降低计算复杂度;

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

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

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

三.实现代码

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

四.体会

这道题因为从来没学过哈希表,或者说学的时候不认真,导致不知道在哈希表中,插入和寻找一个元素的时间复杂度为O(1),因此没想到利用哈希表来搜索与元素相邻的数是否在数组中,使用哈希表是使得算法能够保持在O(n)复杂度的一个必要条件。同时,一个数与其相邻的数都在同一个连续子序列中,因此,可以在进行一次最大连续子序列查找的过程中将所有在该连续子序列中的元素进行标记,从而减少寻找最长连续子序列的这个过程,降低计算复杂度,使得这个寻找过程的计算复杂度为O(n)。

这道题为每一个元素设立标记位来记录其所在的连续子序列已经进行处理的这个思路很巧妙,记得学习借鉴。

版权所有,欢迎转载,转载请注明出处,谢谢

时间: 2024-08-13 19:20:22

LeetCode_Longest Consecutive Sequence的相关文章

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

Binary Tree Longest Consecutive Sequence

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p

Longest Consecutive Sequence 数组连续数字的情况

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

【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

【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

leetcode --day12 Surrounded Regions &amp; Sum Root to Leaf Numbers &amp; Longest Consecutive Sequence

1.  Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. For example, X X X X X O O X X X O X X O X X After running your fu

Binary Tree Longest Consecutive Sequence Leetcode

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p

Leetcode 贪心 Longest Consecutive Sequence

Longest Consecutive Sequence Total Accepted: 19169 Total Submissions: 68303My Submissions 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 consec

[LeetCode] Longest Consecutive Sequence(DP)

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