剑指offer python版 数组中出现次数超过一半的数字

Posted findtruth123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了剑指offer python版 数组中出现次数超过一半的数字相关的知识,希望对你有一定的参考价值。

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版 数组中出现次数超过一半的数字的主要内容,如果未能解决你的问题,请参考以下文章