Two Python solutions: use binary search and sum


  • 0
    J
    def missingNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums = sorted(nums)
        l = len(nums)
        left = 0
        right = l
        
        while (left < right):
            mid = (left + right) / 2
            if mid < nums[mid]:
                right = mid
            else:
                left = mid + 1
        return left
    
    def missingNumber(self, nums):
        n = len(nums)
        return n*(n + 1)/2 - sum(nums)

Log in to reply
 

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