Python O(n) time, O(1) space solutions.

  • 1
    # bit manipulation
    def missingNumber1(self, nums):
        res = 0
        for i in xrange(len(nums)+1):
            res ^= i
        for num in nums:
            res ^= num
        return res
    # math  
    def missingNumber(self, nums):
        return len(nums)*(len(nums)+1)/2 - sum(nums)

Log in to reply

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