Likou 128. The longest continuous sequence (using a hash table to cleverly complete the sorting process)

128. Longest consecutive sequence

Given an unsorted integer array nums, find the length of the longest continuous sequence of numbers (the sequence elements are not required to be continuous in the original array).

Advanced: Can you design and implement a solution with a time complexity of O(n)?

示例 1:

输入:nums = [100,4,200,1,3,2]
输出:4
解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
示例 2:

输入:nums = [0,3,7,2,5,8,4,6,0,1]
输出:9
 

提示:

0 <= nums.length <= 104
-109 <= nums[i] <= 109

answer:

Method 1: Sorting solution

The idea is to use the sorting function to sort the array. If it is continuous, it is very easy to find. Just subtract the two to equal 1, and when it does not match, this segment must have been found, so start looking for the next segment. Finally, compare the longest output.

Code:

int max(int x,int y)
{
    
    
    return x>y?x:y;
}
int cmp(int*x,int*y)
{
    
    
    return *x>*y;
}
int longestConsecutive(int* nums, int numsSize){
    
    
    if(numsSize==0)
        return 0;
    qsort(nums,numsSize,sizeof(int),cmp);
    int length = 1;
    int temp = INT_MIN;
    for(int i=1;i<numsSize;i++)
    {
    
    
        if(nums[i]==nums[i-1])
            continue;
        if(nums[i]-nums[i-1]==1)
        {
    
    
            length++;
        }
        else
        {
    
    
            temp = max(temp,length);
            length = 1;
        }
    }
    temp = max(temp,length);
    return temp;
}

Method 2: Use a hash table to act as sorting (satisfying the time complexity is O(n))

Since the time complexity of O(n) cannot be achieved by using the sorting function, we do not use the sorting function.
But we want to "sort" again, so what should we do?

Can the sorting be done through other thoughts?

We found that the hash table stores the number of occurrences of each number in the array, and we found that the hash table itself is ordered , then we can find that if we first build a hash table, then traverse the original array, and then Store the hash table. At this point, the hash table has been stored, then we find that if we traverse the hash table directly, the "sort" function we want is achieved .
Because if it is continuous, it must be continuous in the hash table .

Therefore, we traverse the hash table directly and start the length calculation. When its storage is not 0, we think that there is the number, so we can participate in the connection; until a certain number is traversed and nothing is stored, then we start a new length calculation, That is, the previous route is deemed to be unable to connect.
However, due to the limitations of the C language, the use of hash tables can only be used for certain situations.

Code:

int max(int x,int y)//这里我写的为数字大小为0-9999的情况
{
    
    
    return x>y?x:y;
}
int longestConsecutive(int* nums, int numsSize){
    
    
    if(numsSize==0)
        return 0;
    int hash[10000] = {
    
    0};
    int length = 0;
    int temp = INT_MIN;
    for(int i=0;i<numsSize;i++)
    {
    
    
        hash[nums[i]]++;
    }
    for(int i=0;i<10000;i++)
    {
    
    
        if(hash[i]!=0)
        {
    
    
            length++;
        }
        else
        {
    
    
            temp = max(temp,length);
            length = 0;
        }
    }
    temp = max(temp,length);
    return temp;
}

Guess you like

Origin blog.csdn.net/xiangguang_fight/article/details/115032950
Recommended