• LeetCode Majority Element Python


    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.

     1 class Solution:
     2     # @param num, a list of integers
     3     # @return an integer
     4     def majorityElement(self, num):
     5         dic = {}
     6         for i in range(len(num)):
     7             if (num[i]) not in dic:
     8                 dic[num[i]] = 1
     9             else:
    10                 dic[num[i]] += 1
    11         l = [(a,b) for a, b in dic.items()]
    12         return (sorted(l,key = lambda x:x[1]))[-1][0]
    13         
  • 相关阅读:
    书_Delphi
    20160226
    SVG_style_script
    辅助
    电影_Z
    Windows下软件调试
    20160221
    Qt5.3.2_vs10_发布时所需DLL的路径
    android intent 传数据
    android 消息机制
  • 原文地址:https://www.cnblogs.com/fyymonica/p/4370966.html
Copyright © 2020-2023  润新知