lintcode 46.主元素

class Solution:
    """
    @param: nums: a list of integers
    @return: find a  majority number
    """
    def majorityNumber(self, nums):
        # write your code here
        A = None
        count = 0
        nums.sort()
        for ele in nums:
            if count == 0:
                A = ele
                count = count + 1
            elif A == ele:
                count += 1
            elif A != ele:
                count -= 1
        return A

你可能感兴趣的:(python,lintcode)