Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Because we know that nums contains nn numbers and that it is missing exactly one number on the range [0..n-1][0..n−1], we know that nn definitely replaces the missing number in nums. Therefore, if we initialize an integer to nn and XOR it with every index and value, we will be left with the missing number. Consider the following example (the values have been sorted for intuitive convenience, but need not be):
- Index 0 1 2 3
- Value 0 1 3 4
- missing = 4∧(0∧0)∧(1∧1)∧(2∧3)∧(3∧4)
- = (4∧4)∧(0∧0)∧(1∧1)∧(3∧3)∧2
- = 0∧0∧0∧0∧2=2
- """
- class Solution(object):
- def missingNumber(self, nums):
- """
- :type nums: List[int]
- :rtype: int
- """
- missing = len(nums)
- for i in range(0, len(nums)):
- missing = missing ^ i ^ nums[i]
- return missing
Advertisement
Add Comment
Please, Sign In to add comment