O(n) Solution in C


  • 0
    B
    int* findErrorNums(int* nums, int numsSize, int* returnSize) {
        int i = 0, a[10001] = {0}, repeat = 0, deficiency = 0, current = 0;
        int *result = (int *)malloc(sizeof(int) * 2);
        *returnSize = 2;
        for( ; i < numsSize; i++){
            current = *(nums + i);
            a[current]++;
        }
        for(i = 1; i <= numsSize; i++){
            current = a[i];
            if(current == 0) { deficiency = i; }
            if(current == 2) { repeat = i; }
        }     
        *result = repeat, *(result + 1) = deficiency;
        return result;
    }
    

Log in to reply
 

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