Article directory
One [topic category]
- hash table
Two [question difficulty]
- medium
Three [topic number]
- 128. Longest Consecutive Sequence
Four [title description]
- Given an unsorted integer array nums, find the length of the longest sequence of consecutive numbers (it is not required that the elements of the sequence are consecutive in the original array).
- Please design and implement an algorithm with O(n) time complexity to solve this problem.
Five [topic examples]
-
Example 1:
- Input: nums = [100,4,200,1,3,2]
- Output: 4
- Explanation: The longest continuous sequence of numbers is [1, 2, 3, 4]. It has length 4.
-
Example 2:
- Input: nums = [0,3,7,2,5,8,4,6,0,1]
- Output: 9
Six [topic prompt]
- 0 < = n u m s . l e n g t h < = 1 0 5 0 <= nums.length <= 10^5 0<=nums.length<=105
- − 1 0 9 < = n u m s [ i ] < = 1 0 9 -10^9 <= nums[i] <= 10^9 −109<=nums[i]<=109
Seven [problem-solving ideas]
- The idea of using hash tables
- First add the elements in the array to the hash table, the purpose of this is to remove duplication, and to facilitate subsequent calculations
- Then traverse each element in the hash table, calculate the continuous array starting from this element, and scan through the hash table
- This way we only use O ( n ) O(n)O ( n ) time complexity to solve this problem
- Finally return the result
Eight 【Time Frequency】
- Time complexity: O ( n ) O(n)O(n), n n n is the length of the incoming array
- Space Complexity: O ( n ) O(n)O(n), n n n is the length of the incoming array
Nine [code implementation]
- Java language version
class Solution {
public int longestConsecutive(int[] nums) {
HashSet<Integer> map = new HashSet<>();
for(int i = 0;i < nums.length;i++){
map.add(nums[i]);
}
int res = 0;
for(int num : map){
if(!map.contains(num - 1)){
int curNum = num;
int curLen = 1;
while(map.contains(curNum + 1)){
curNum += 1;
curLen += 1;
}
res = Math.max(res, curLen);
}
}
return res;
}
}
- C language version
struct HashNode
{
int key;
UT_hash_handle hh;
};
int longestConsecutive(int* nums, int numsSize)
{
struct HashNode* map = NULL;
struct HashNode* tempNode = NULL;
for(int i = 0;i < numsSize;i++)
{
HASH_FIND_INT(map, &nums[i], tempNode);
if(tempNode == NULL)
{
struct HashNode* newNode = (struct HashNode*)malloc(sizeof(struct HashNode));
newNode->key = nums[i];
HASH_ADD_INT(map, key, newNode);
}
}
struct HashNode *val, *temp;
int res = 0;
HASH_ITER(hh, map, val, temp)
{
if(val != NULL)
{
int lastVal = val->key - 1;
HASH_FIND_INT(map, &lastVal, tempNode);
if(tempNode == NULL)
{
int curNum = val->key + 1;
int curLen = 1;
HASH_FIND_INT(map, &curNum, tempNode);
while(tempNode != NULL)
{
curNum += 1;
curLen += 1;
HASH_FIND_INT(map, &curNum, tempNode);
}
res = fmax(res, curLen);
}
}
}
HASH_ITER(hh, map, val, temp)
{
HASH_DEL(map, val);
free(val);
}
return res;
}
- Python language version
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
map = set(nums)
res = 0
for num in map:
if num - 1 not in map:
curNum = num
curLen = 1
while curNum + 1 in map:
curNum += 1
curLen += 1
res = max(res, curLen)
return res
- C++ language version
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> map;
for(int i = 0;i < nums.size();i++){
map.insert(nums[i]);
}
int res = 0;
for(const int& num : map){
if(!map.count(num - 1)){
int curNum = num;
int curLen = 1;
while(map.count(curNum + 1)){
curNum += 1;
curLen += 1;
}
res = max(res, curLen);
}
}
return res;
}
};
Ten【Submission Results】
-
Java language version
-
C language version
-
Python language version
-
C++ language version