A O(n) solution and a constant-space solution both accepted as best submission in C


  • 1
    //AC - 12ms - using O(n) space;
    int missingNumber0(int* nums, int size)
    {
        bool *arr = (int*)malloc(sizeof(bool)*(size+1));
        memset(arr, 0, sizeof(bool)*(size+1));
        for(int i = 0; i < size; i++)
            arr[nums[i]] = true;
        for(int i = 0; i <= size; i++)
            if(!arr[i])
                return i;
    }
    
    //AC - 12ms;
    //a^a=0 and 0^b=b;
    int missingNumber(int* nums, int size)
    {
        int t = size;
        for(int i = 0; i < size; i++)
            t ^= nums[i]^i;
        return t;
    }

Log in to reply
 

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