defsingleNumber(self,nums):dict={}for i in nums:if i notindict:dict[i]=1else:dict[i]+=1for key, value indict.items():if value==1:return key
return0
2 排序比较判断
defsingleNumber(self,nums):
nums.sort()
length =len(nums)if length <3:return nums[0]
i =0while i < length -2:if nums[i]!= nums[i+1]:return nums[i]else:
i +=2return nums[-1]
3 集合交叉法
defsingleNumber(self, nums):
nums.sort()
n =list(set(nums[::2])-set(nums[1::2]))[0]return n
题目II
python代码
defsingleNumber(self,nums):dict={}for i in nums:if i notindict:dict[i]=1else:dict[i]+=1for key, value indict.items():if value==1:return key
return0
题目III
python代码
defsingleNumber(self,nums):dict={}
nums_new=[]for i in nums:if i notindict:dict[i]=1else:dict[i]+=1for key, value indict.items():if value==1:
nums_new.append(key)return nums_new