python 求众数 LeetCode N0.169

系统 1524 0

python 求众数 LeetCode N0.169 python 求众数 LeetCode N0.169_第1张图片

这道题有很多解法官方leetcode上面是六种,由于说的太过于详细,我都不好意思,再补充什么了。所以我就写了一点,没看答案之前的写法,和我觉得,需要掌握的写法吧。他写的很多代码很精简,值得学习。(ps,纳闷的是,即使我用的O(n)的复杂度,排名也很靠后哈哈哈哈哈)

            
              
                class
              
              
                Solution
              
              
                (
              
              
                object
              
              
                )
              
              
                :
              
              
                def
              
              
                majorityElement
              
              
                (
              
              self
              
                ,
              
               nums
              
                )
              
              
                :
              
              
                """
        :type nums: List[int]
        :rtype: int
        """
              
              
                #count = 0
              
              
                #candidate = None
              
              
                #for num in nums:
              
              
                #   if count == 0:
              
              
                #        candidate = num
              
              
                #    count += (1 if num == candidate else -1)
              
              
                #return candidate
              
              

        count 
              
                =
              
              
                0
              
              
        candidate 
              
                =
              
               nums
              
                [
              
              
                0
              
              
                ]
              
              
                for
              
               i 
              
                in
              
              
                range
              
              
                (
              
              
                len
              
              
                (
              
              nums
              
                )
              
              
                )
              
              
                :
              
              
                if
              
               nums
              
                [
              
              i
              
                ]
              
              
                ==
              
               candidate
              
                :
              
              
                count 
              
                +=
              
              
                1
              
              
                else
              
              
                :
              
              
                count 
              
                -=
              
              
                1
              
              
                if
              
               count 
              
                ==
              
              
                0
              
              
                :
              
              
                candidate 
              
                =
              
              nums
              
                [
              
              i
              
                +
              
              
                1
              
              
                ]
              
              
                return
              
               candidate

            
          
            
              
                class
              
              
                Solution
              
              
                (
              
              
                object
              
              
                )
              
              
                :
              
              
                def
              
              
                majorityElement
              
              
                (
              
              self
              
                ,
              
               nums
              
                )
              
              
                :
              
              
                """
        :type nums: List[int]
        :rtype: int
        """
              
              
                import
              
               math
        count 
              
                =
              
              
                {
              
              
                }
              
              
                if
              
              
                len
              
              
                (
              
              nums
              
                )
              
              
                %
              
              
                2
              
              
                ==
              
              
                1
              
              
                :
              
              
            n 
              
                =
              
              
                len
              
              
                (
              
              nums
              
                )
              
              
                +
              
              
                1
              
              
                else
              
              
                :
              
              
            n 
              
                =
              
              
                len
              
              
                (
              
              nums
              
                )
              
              
                for
              
               i 
              
                in
              
               nums
              
                :
              
              
            count
              
                [
              
              i
              
                ]
              
              
                =
              
               count
              
                .
              
              get
              
                (
              
              i
              
                ,
              
              
                0
              
              
                )
              
              
                +
              
              
                1
              
              
                if
              
               count
              
                [
              
              i
              
                ]
              
              
                >=
              
               n
              
                /
              
              
                2
              
              
                :
              
              
                return
              
               i
        
              
                for
              
               i 
              
                in
              
               nums
              
                :
              
              
                if
              
               count
              
                [
              
              i
              
                ]
              
              
                >=
              
               n
              
                /
              
              
                2
              
              
                :
              
              
                return
              
               i

            
          
            
              
                class
              
              
                Solution
              
              
                :
              
              
                def
              
              
                majorityElement
              
              
                (
              
              self
              
                ,
              
               nums
              
                )
              
              
                :
              
              
        counts 
              
                =
              
               collections
              
                .
              
              Counter
              
                (
              
              nums
              
                )
              
              
                return
              
              
                max
              
              
                (
              
              counts
              
                .
              
              keys
              
                (
              
              
                )
              
              
                ,
              
               key
              
                =
              
              counts
              
                .
              
              get
              
                )
              
              
                #作者:LeetCode
              
              
                #链接:https://leetcode-cn.com/problems/majority-element/solution/qiu-zhong-shu-by-leetcode-2/
              
              
                #来源:力扣(LeetCode)
              
              
                #著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
              
            
          

更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论