当前位置:诺佳网 > 电子/半导体 > 区块链 >

【LintCode 简单】46. 主元素

时间:2018-01-17 | 栏目:区块链 | 点击:

1.问题描述:

给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一。


2.样例:

给出数组[1,1,1,1,2,2,2],返回 1。


3.代码:

class Solution: """ @param: nums: a list of integers @return: find a majority number """ def majorityNumber(self, nums): # write your code here length=len(nums) for i in range(int(length/2)+1): count=1 for j in range(i+1,length): if nums[i] == nums[j]: count+=1 if count>length/2: return nums[i]

您可能感兴趣的文章:

相关文章