137.只出现一次的数字
题目难度:中等
题目:原题链接
思路:
1、原题目中重复的数字都是出现三次,我们只需要找出出现一次的就可以;
2、用python写的正好可以利用字典;
3、如果你其他语言,用哈希表一样的;
4、暴力法,遍历;
下面给出字典方法
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
#字典法统计出现的次数,只要返回出现一次的就可以了
resdict = {}
for num in nums:
if num not in resdict:
resdict[num] = 1
else:
resdict[num] +=1
return min(resdict.items(),key = lambda x:x[1])[0]