微信红包

来源:互联网 发布:印尼打车软件 编辑:程序博客网 时间:2024/04/29 15:15

春节期间小明使用微信收到很多个红包,非常开心。在查看领取红包记录时发现,某个红包金额出现的次数超过了红包总数的一半。请帮小明找到该红包金额。写出具体算法思路和代码实现,要求算法尽可能高效。
给定一个红包的金额数组gifts及它的大小n,请返回所求红包的金额。

class Gift:    def getValue(self, gifts, n):         # write code here        times = {}        for count in gifts:            if not times.has_key(count):                times[count] = 1             else:                times[count] += 1        hav = n/2         for (k,v) in times.items():            if v > hav:                return k        return 0#test = [37256,61520,11540,61520,69323,61520,61520]test = [26316,43072,60522,60522,60522,92556,60522,92883,60522,60522,60522,78468,77779,6925,60522,60522,14543,72687,60522,60693,60522,67024,60522,53203,51914,60522,60522,63132,60522,1678,60522,60522,51411,14746,60522,60522,3988,74682,34876,28533,60522,81241,52488,60381,51210,60522,60522,82673,9396,60522,57140,60504,24174,9476,77156,7136,60522,60522,60522,60522,60522,60522,60522,60522,60522,84283,34490,60522,60522,96049,89339,30519,60522,25316,60522,82942,60522,25097,17830,60522,34192,28994,60522,86767,93688,60522,60522,91468,60522,60522,60522,60522,60522,60522,52131,813,60522,42362,60522,60522,18405,67642,60522,14263,60522,60522,60522,78908,48847,60522,70071,10752,9129,1164,39777,60522,60522,60522,94244,60522,60522,60522,25713,35431,60522,67928,60522,60522,88514,60522,60522,63001,60522,53716]gift = Gift()print gift.getValue(test,134)
0 0
原创粉丝点击