268. Missing Number
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 70% Topics: Array, Hash Table, Math, Binary Search, Bit Manipulation, Sorting
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
import operator
class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return reduce(operator.xor, nums,
reduce(operator.xor, xrange(len(nums) + 1)))
class Solution2(object):
def missingNumber(self, nums):
return sum(xrange(len(nums)+1)) - sum(nums)
Solution from kamyu104/LeetCode-Solutions · MIT