题解 | #字符串的排列#001
数组中出现次数超过一半的数字
http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163
-- coding:utf-8 --
class Solution: def MoreThanHalfNum_Solution(self, numbers): # write code here
ret = None
cnt = 0
pre = None
for idx,item in enumerate(numbers):
if cnt==0:
ret = item
cnt = 1
pre = ret
else:
if item == pre:
cnt += 1
else:
cnt -= 1
if numbers.count(ret)<=len(numbers)/2:
return 0
return ret