Python solution faster than 90%


  • 0
    U

    majority is the candidate major element,times is the voting weight

    class Solution(object):
        def majorityElement(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            times = 0
            majority = nums[0]
            for x in nums:
                if x == majority:
                    times += 1
                elif times == 0:
                    majority = x
                    times = 1
                else:
                    times -= 1
            return majority
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.