1. 程式人生 > >劍指offer python版 數組中出現次數超過一半的數字

劍指offer python版 數組中出現次數超過一半的數字

pre list false else spa lse code 數字 pri

def aa(nums):
    if not nums:
        return False
    hashes={}
    ret=[]
    for s in nums:
        hashes[s]=hashes[s]+1 if hashes.get(s) else 1
        if hashes[s] >len(nums)/2:
            ret.append(s)
            
    return list(set(ret))


print(aa([1,2,3,2,2,2,2,2,3,2,3,3,4]))
            

劍指offer python版 數組中出現次數超過一半的數字