C语言出现几次都可以检测出来,不过需要额外分配内存


  • 0
    C

    int singleNumber(int* nums, int numsSize) {
    int max = nums[0];
    int min = nums[0];
    int i = 0;
    for (i = 1; i < numsSize; i++)
    {
    min = nums[i]<min ? nums[i] : min;
    max = nums[i]>max ? nums[i] : max;
    }
    int* a = (int*)malloc(sizeof(int)*(max - min + 1));
    memset(a, 0, max - min + 1);
    if (a)
    {
    for (i = 0; i<numsSize; ++i)
    {
    a[nums[i] - min]++;
    }
    for (i = 0; i < max-min+1; i++)
    {
    if (a[i] == 1)
    {
    return i;
    }
    }
    }
    return -1;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.