[8种方法]169 Majority Element

  • 分类:分治/sort/位运算/vote/HashMap/Random
方法+时间/空间复杂度

169. Majority Element

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

Example 1:

Input: [3,2,3]
Output: 3

Example 2:

Input: [2,2,1,1,1,2,2]
Output: 2

代码:

HashTable方法:

class Solution:
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums)==0:
            return -1
        
        count_table={}
        len_=len(nums)
        for i in range(len_):
            if nums[i] not in count_table:
                count_table[nums[i]]=1
            else:
                count_table[nums[i]]+=1
                
            if count_table[nums[i]]>len_//2:
                return nums[i]
        
        return -1

随机数方法:

from random import randint

class Solution:
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums)==0:
            return -1
        
        len_=len(nums)
        
        while (1):
            my_num=nums[randint(0,len_-1)]
            count=0
            for n in nums:
                if n==my_num:
                    count+=1
                    if count>len_//2:
                        return my_num
                
        return -1

随机数方法:

from random import randint

class Solution:
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums)==0:
            return -1
        
        len_=len(nums)
        
        while (1):
            my_num=nums[randint(0,len_-1)]
            count=0
            for n in nums:
                if n==my_num:
                    count+=1
                    if count>len_//2:
                        return my_num
                
        return -1

Boyer-Moore Vote方法:

class Solution:
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums)==0:
            return -1
        
        len_=len(nums)
        
        my_num=nums[0]
        count=0
        for n in nums:
            if n==my_num:
                count+=1
            else:
                count-=1
                if count==0:
                    my_num=n
                    count=1
                
        return my_num

位运算方法:

class Solution:
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums)==0:
            return -1
        
        len_=len(nums)
        my_num=0
        
        for i in range(32):
            mask= 1 << i
            count=0
            for n in nums:
                if (n & mask):
                    count+=1
                    
            if count>len_//2:
                if i==31:
                    # if i==31,it means it is a negative number
                    my_num = -((1<<31)-my_num)
                else:
                    my_num|=mask
                
        return my_num

简单sort方法:

class Solution:
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums)==0:
            return -1
                
        return sorted(nums)[len(nums)//2]

分治方法:

class Solution:
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums)==0:
            return -1
        elif len(nums)==1:
            return nums[0]
        
        len_=len(nums)
        left=self.majorityElement(nums[:len_//2])
        right=self.majorityElement(nums[len_//2:])
        
        if left==right:
            return left
        else:
            count=0
            for n in nums:
                if n==left:
                    count+=1
            if count>len_//2:
                return left
            else:
                return right

讨论:

1.这道题咱们的速度图就不发了,毕竟这个题的算法特别多。
2.reference:花花酱
3.方法2BST和方法7部分排序法是C++自带方法,Python没有我就忽略,不实现了

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,196评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,559评论 0 23
  • 从前山里有座庙 我汗如雨下,不知道已爬了多少层台阶。山顶的寺庙仍遥不可及。我把手里的长剑往旁边一扔,“大爷的,老子...
    猫自在阅读 3,785评论 6 6
  • 大雨是画师 把闪烁颜料洒在深蓝幕布上 便有了万千颗星星✨ 大雨是丘比特 他怎知 今夜的你 也在抬头望夜空
    缀孟阅读 1,526评论 0 0
  • 今天到目前还没有好好把得到今天更新补充的内容只字不差的看完。但又快到我规定的23点睡眠时间了。最近给师妹改文章...
    董文娇阅读 1,108评论 0 0

友情链接更多精彩内容