39 数组超过一半的数字

2018-06-21  本文已影响0人  土味老猪
class Solution():
    def morethanhalf(self,A):
        count = 1
        ans = A[0]
        for i in range(len(A)-1):
            if count == 0:
                count = 1
                ans = A[i+1]

            if A[i] == A[i+1]:
                count+=1
            else:
                count-=1

        return ans

s = Solution()
print(s.morethanhalf([1,2,3,2,2,2,5,3,2]))

上一篇 下一篇

猜你喜欢

热点阅读