题解 | #微信红包#

微信红包

https://www.nowcoder.com/practice/fbcf95ed620f42a88be24eb2cd57ec54

# -*- coding:utf-8 -*-

class Gift:
    def getValue(self, gifts, n):
        # write code here
        count=1
        candidate=gifts[0]
        for i in gifts[1:]:#从第二个元素开始
            if i==candidate:
                count+=1
            elif count==0:
                candidate=i
                count=0
            else:
                count-=1
        count=0
        for i in gifts:
            if i==candidate:
                count+=1
                if count>n/2:
                    return candidate
        return 0



            

全部评论

相关推荐

09-01 21:40
已编辑
同济大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务