Leetcode 268 Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
For example,
Given nums = [0, 1, 3] return 2.
1.XOR
class Solution(object): def missingNumber(self, nums): t = len(nums) for i in range(len(nums)): t ^= i t ^= nums[i] return t
2.SUM
class Solution(object): def missingNumber(self, nums): return len(nums)*(len(nums)+1)/2-sum(nums)
浙公网安备 33010602011771号