Online tester is not correct, corner case not considered


  • 0
    G

    I'm wondering if anyone mentioned this before, but I just realized that for some corner cases such as [0,1,2] where no number is actually missing, the algorithm of online tester will consider nums.length as the missing number. (i.e. 3 is the missing number for the example above)

    I don't think such a corner case was correctly treated. If there's no number missing, return -1 will be a more reasonable output.

    Below is my code:

        {  
            // some change to handle the case that no number is missing
            int result = 0;
            for (int i = 0; i < nums.length; i++)
            {
                result ^= i;
                result ^= nums[i];
            }
            result ^= nums[nums.length - 1];
            
            if (result == nums[nums.length - 1])
                return -1;
            else
                return result; 
        }
    

Log in to reply
 

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