【力扣 - 最长连续数组】

题目描述

给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。

请你设计并实现时间复杂度为 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 <= 10^5

-10^9 <= nums[i] <= 10^9

题解

思路

先排序再比较计数;

如果相邻两数差一计数,如果相等进入下一层循环判断;

如果后面数与前一个数既不相等又不比前一个多一,重值计数为1.

代码

c 复制代码
int cmp(const void* a, const void* b)
{
    // Custom comparison function for qsort to sort integers in ascending order
    return (long long)*(int*)a - (long long)*(int*)b;
}

int longestConsecutive(int* a, int n)
{
    // Check for edge cases
    if (a == NULL || n == 0) {
        return 0;
    }
    
    /* The  qsort  function in C is used to sort an array in ascending order. 
     * It takes the following parameters: 
     * base: Pointer to the array to be sorted. 
     * nmemb: Number of elements in the array. 
     * size: Size in bytes of each element in the array. 
     * compar: Pointer to a comparison function that determines the order of elements. 
     * The comparison function ( compar ) should return an integer less than, 
     * equal to, or greater than zero if the first argument is considered to be 
     * respectively less than, equal to, or greater than the second argument. 
     * void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
     */
    // Sort the input array 'a' in ascending order
    qsort(a, n, sizeof(int), cmp);
    
    int t = a[0]; // Initialize 't' with the first element of the sorted array
    int cnt = 1; // Initialize 'cnt' to keep track of the current consecutive sequence length
    int max = 1; // Initialize 'max' to keep track of the maximum consecutive sequence length
    
    // Iterate through the sorted array to find the longest consecutive sequence
    for (int i = 1; i < n; i++) {
        if (a[i] == t) {
            // If the current element is equal to the previous element, skip it
            continue;
        } else if ((long long)a[i] - t == 1LL) {
            // If the current element is consecutive to the previous element
            t = a[i]; // Update 't' to the current element
            cnt++; // Increment the consecutive count
            if (max < cnt) {
                max = cnt; // Update 'max' if a longer consecutive sequence is found
            }
        } else {
            // If the current element is not consecutive to the previous element
            t = a[i]; // Update 't' to the current element
            cnt = 1; // Reset the consecutive count
        }
    }
    
    return max; // Return the maximum consecutive sequence length
}
相关推荐
lizz3123 分钟前
机器学习中的线性代数:奇异值分解 SVD
线性代数·算法·机器学习
_星辰大海乀25 分钟前
LinkedList 双向链表
java·数据结构·链表·list·idea
MSTcheng.28 分钟前
【C语言】动态内存管理
c语言·开发语言·算法
不去幼儿园32 分钟前
【启发式算法】Dijkstra算法详细介绍(Python)
人工智能·python·算法·机器学习·启发式算法·图搜索算法
serve the people40 分钟前
神经网络中梯度计算求和公式求导问题
神经网络·算法·机器学习
闻缺陷则喜何志丹1 小时前
【二分查找、滑动窗口】P10389 [蓝桥杯 2024 省 A] 成绩统计|普及+
c++·算法·蓝桥杯·二分查找·滑动窗口·洛谷·成绩
星空露珠1 小时前
迷你世界脚本显示板管理接口:DisPlayBoard
数据结构·游戏·lua
乔冠宇2 小时前
蓝桥杯算法——铠甲合体
算法·职场和发展·蓝桥杯
商bol452 小时前
算阶,jdk和idea的安装
数据结构·c++·算法
迷迭所归处2 小时前
C语言 —— 愿文明如薪火般灿烂 - 函数递归
c语言·开发语言·算法