天天看點

劍指offer3:數組中重複的數字

#方法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)