day15笔记
1.题目描述
2.代码构思(debug)
方法1:位运算
def singleNumber(self, nums: List[int]) -> int:
if not nums:
return
num = nums[0]
for item in nums[1:]:
num = num^item
return num
方法2:hash方法
class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
if not nums:
return
d = {}
for item in nums:
if item not in d:
d[item] = 1
else:
d[item] += 1
res = []
for k,v in d.items():
if v == 1:
res.append(k)
return res