题意:

  给一个数组,其中有一个元素的出现次数已经超过数组的一半大小,请找出这个元素?

思路:

  可以扫一遍数组,将这个出现次数过多的元素抵消其他的元素,最后必定留下1个以上的元素,就是它自己了。

python3

扫一遍

 class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
idx,cnt,cur=0,0,0
while idx<len(nums):
idx+=1
if cnt==0:
cnt=1
cur=nums[idx-1]
continue
if nums[idx-1]!=cur:
cnt-=1
else:
cnt+=1
return cur

AC代码

排序

 class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return sorted(nums)[int(len(nums)/2)]

AC代码

dict

 class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dic={}
for x in nums:
dic[x]=dic.get(x,0)+1
if dic[x]*2>len(nums):
return x

AC代码

LeetCode Majority Element(简单题)的更多相关文章

  1. 2016.5.18——leetcode:Majority Element

    Majority Element 本题收获: 1.初步了解hash,nth_element的用法 2.题目的常规思路 题目: Given an array of size n, find the ma ...

  2. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  3. LeetCode Majority Element I && II

    原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...

  4. [LeetCode] Majority Element II 求大多数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...

  5. LeetCode Majority Element I

    原题链接在这里:https://leetcode.com/problems/majority-element/ 题目: Given an array of size n, find the major ...

  6. [LeetCode] Majority Element 求众数

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  7. [LeetCode] Majority Element 求大多数

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. LeetCode——Majority Element

    在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素.容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element. ...

  9. LeetCode Majority Element Python

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. CSS 框模型——规定了元素框处理元素内容、内边距、边框和外边距的方式

    转自:http://www.w3school.com.cn/css/css_boxmodel.asp 要知道在父元素:float, rel, abs位置情况下,box模型的变换情况,请见:http:/ ...

  2. urlencode在url中的作用

    urlencode编码能解决特殊字符的传输问题. 使用urlencode主要用于正常识别输入的汉字.空格以及其他特殊字符. 列如: 一产品名称为A&T Plastic,在产品列表中就产生了这样 ...

  3. visio画UML用例图没有include关系的解决方法

    今天用Microsoft Visio画用例图时,发现visio UML用例里面找不到include关系,即“箭头”+“<>” 这个组件,后来终于发现一个可行的解决办法: 首先:打开Micr ...

  4. 嵌套 click 第二层 click会叠加 导致 触发 多次

    $("#appearHiddenDiv").click(function(){ $("#hiddenDiv").css({display:"block ...

  5. 1.精通前端系列技术之js正则表达式

    在不会正则的时候,我们寻找字符串某些规律或者截取部分特殊字符的时候,我们需要写很多行代码来获取我们想要的字符串,在使用正则之后,代码量会大量简洁很多 1.字符串的比较,判断是否数字类型的字符串,我们用 ...

  6. c#读取文本文档实践1-File.ReadAllLines()

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  7. 红帽中出现”This system is not registered with RHN”的解决方案

    原因是你的linux没有在红帽网络上注册,所以无法下载上面的软件包,替代方案可以使用centos. 下面介绍下使用centos 的流程 1.卸载rhel的默认安装的yum包查看yum包rpm -qa| ...

  8. 关于Warn:name or service not known的解决办法

    由于之前搭建起了一个集群,然后直接将相应的配置文件复制过来 , 发现出现了 Warn:name or service not known 的问题,导致无法启动datanode. 解决的办法就是将sal ...

  9. String的两个API,判断指定字符串是否包含另一字符串,在字符串中删除指定字符串。

    // 在字符串中删除指定字符串. String phoneNum="1795112345"; phoneNum = phoneNum.replace("17951&quo ...

  10. vc设置按钮文字颜色

    设置按钮文字颜色使用 CMFCBUTTON即可 在OnInitDialog函数加入如下内容即可 ((CMFCButton*)GetDlgItem(IDC_MFCBUTTON1))->SetTex ...