One line Python solution


  • 0
    C
    def missingNumber(nums):
            return reduce(lambda x, y: x ^ y, nums) ^ reduce(lambda x, y: x ^ y, range(len(nums)+1))
    

    The basic idea is to use bit manipulation. Imagine that there is a big list equals to nums + [n for n in xrange(len(nums) + 1)], we can xor all the items in the big list, those items that appear twice will xor themselves and turns into zero, therefor the result will be the number that we are seeking.


  • 0

    What's the point of your [n for n in ...]?


  • 0
    C

    It's the list contains all the numbers from 0-n, should be range(len(nums)+1), that's clearer, I've updated my post, thanks!


Log in to reply
 

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