**一. 题目描述**
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.
**二. 题目分析**
这道题有两个技巧:
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(n)。
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016腾讯软件开发笔试题)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契数列问题)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波兰式的计算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和问题)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching