My AC O(n) solution in python, but i don't think it's fast enough


  • -1
    N
    class Solution:
            # @param num, a list of integers
            # @return an integer
            def majorityElement(self, num):
                temp=[]
                for i in num:
                    if not temp or temp[-1]==i:
                        temp.append(i)
                    else:
                        temp.pop()
                return temp[0]
    

    #solution:
    ###two different number make a pair, after making the total list do the match, the majority number must have at least 1 element that can not match. the rest element(s) that not matched is the majority number.
    ###here i use a stack to solve the problem, it takes 356 ms, i don't think it's fast enough


Log in to reply
 

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