当前位置: 代码迷 >> 综合 >> 剑指offer3:数组中重复的数字
  详细解决方案

剑指offer3:数组中重复的数字

热度:45   发布时间:2024-01-25 02:30:25.0

#方法1:直接排序后,找到重复的 时间复杂度 o(nlogn)
li=[2,8,4,6,9,1,3,2]
l1=sorted(li)
for i in range(len(l1)-1):
if l1[i]==l1[i+1]:
print(l1[i])
#方法2:hash 表


#方法2:hash 表
li=[2,8,4,6,9,1,3,2]
def repeat(li):
count=1
temp={}
for i in range(len(li)):
if li[i] in temp:
temp[li[i]]+=1
else:
temp[li[i]]=1
for k,v in temp.items():
if v>1:
return k
repeat(li)

  相关解决方案