def missingNumber(self, nums): """ :type nums: List[int] :rtype: int """ if nums==None or len(nums)==0: return -1 return len(nums) * (len(nums)+1) / 2 - sum(nums)
位运算
0-n异或,再对所有数字异或
代码
1 2 3 4 5 6 7 8 9 10 11 12 13
class Solution(object):
def missingNumber(self, nums): """ :type nums: List[int] :rtype: int """ res = 0 for i in range(len(nums)+1): res ^= i for num in nums: res ^= num return res
评论系统未开启,无法评论!