quite simple C solution without extra space


  • 0
    X

    // quick sort comparison function
    int cmp(const void *a , const void *b)
    {
    return * (int *)a - * (int *)b ;
    }

    int* findDisappearedNumbers(int* nums, int numsSize, int* returnSize) {

    int *disappear = (int*)calloc(numsSize,sizeof(int));
    qsort(nums,numsSize,sizeof(int),cmp); 
    
    *returnSize =0;
    int i=0 , j=1 , k=0 ;
    while(j<=numsSize)
    {
        if(nums[i]==j)
        {
            while(nums[i]==j) //skip the duplicate number
               i++;
            j++;
        }
        else
        {
            (*returnSize)++;
            disappear[k++] = j++;
        }
    }
    return disappear;
    

    }


Log in to reply
 

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