mycode 80.25%
class Solution(object): def missingNumber(self, nums): """ :type nums: List[int] :rtype: int """ Max = len(nums) t = {i for i in range(Max+1)} f = set(nums) return (t - f).pop()
参考
def missingNumber(nums): """ :type nums: List[int] :rtype: int """ res = len(nums) print(res) for i in range(len(nums)): res ^= (i ^ nums[i]) print(i,nums[i],i^nums[i],res) return res #最初设置返回值res是nums的长度,目标值i^现有值nums[i],那么如果i和nums[i]是相同的,异或的结果就是0,再让res^该结果,那么res仍等于res,这样说明i这个数是存在的,也就是说所有相同的数都被变成了0,剩下的就是缺失的数