share 3 python solutions


  • 0
    H
    # xor
    class Solution(object):
        def missingNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            xor, i = 0, 0
            while i < len(nums):
                xor = xor ^ i ^ nums[i]
                i += 1
            return xor ^ i
    
    # sum
    class Solution(object):
        def missingNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            n = len(nums)
            sums = (0 + n) * (n + 1) // 2
            for num in nums:
                sums -= num
            return sums
    
    # sort
    class Solution(object):
        def missingNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            nums.sort()
            i = 0
            for num in nums:
                if num != i:
                    return i
                i += 1
            return i
    

Log in to reply
 

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