#第一种方法:直接判断集合长度
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
len1=len(nums)
len2=len(set(nums))
if len1==len2:
return False
else:
return True
#2.使用hash表
#首先判断元素长度是否为0
if len(nums)==0:
return False
hashtable={}
for num in nums:
if num not in hashtable.keys():
hashtable[num]=1
else:
#当需要得到次数时用这个
# temp=hashtable.values
# hashtable[num]=temp+1
return True
return False
#统计两个字符串中元素出现的个数 自己写的还是很复杂
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
hashtable1={}
hashtable2={}
for i in s:
if i not in hashtable1.keys():
hashtable1[i]=1
else:
temp=hashtable1[i]
hashtable1[i]=temp+1
for j in t:
if j not in hashtable2.keys():
hashtable2[j]=1
else:
temp=hashtable2[j]
hashtable2[j]=temp+1
for key1 in hashtable1.keys():
for key2 in hashtable2.keys():
if key1==key2:
hashtable2[key2]=hashtable2[key2]-hashtable1[key1]
for keys,values in hashtable2.items():
if values!=0:
return keys