This is a basic solution, but always shows time exceeds, is there something wrong with my code?


  • 0
    Y

    class Solution:
    # @param {integer[]} nums
    # @return {integer}
    def majorityElement(self, nums):

        for i in range(0,len(nums)):
            count=1
            key = nums[i]
            for j in range(i+1,len(nums)):
                
                if key == nums[j]:
                    count += 1
                
            if count > len(nums)/2:
                return key

Log in to reply
 

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