题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) O(n2)
blablabla
时间复杂度分析:blablabla
P 代码
class Solution(object):
def moreThanHalfNum_Solution(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return
if len(nums) == 1:
return nums[0]
res = nums[0]
n = 1
for i in range(1, len(nums)):
if nums[i] == res:
n += 1
else:
n -= 1
if n <= 0:
res = nums[i]
n = 1
return res