题目链接:https://leetcode.com/problems/majority-element/

算法类型:分治法

题目分析:获取长度为n的数组中的众数(出现次数大于等于⌊ n/2 ⌋)

代码实现:

class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums_len = len(nums)
if nums_len == 1:
return nums.pop()

part_one_majory = self.majorityElement(nums[0: (nums_len / 2)])
part_two_majory = self.majorityElement(nums[(nums_len / 2): nums_len])

if part_one_majory == part_two_majory:
return part_one_majory

count_one = nums.count(part_one_majory)
count_two = nums.count(part_two_majory)
if count_one > count_two:
return part_one_majory
return part_two_majory

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 求众数

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

  4. LeetCode Majority Element I && II

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

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

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

  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 I

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

  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 ...

  10. Leetcode: Majority Element II

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

随机推荐

  1. yii2 随笔

    view 1.添a标签 use yii\helpers\Html; //需要引用html组件 <?= Html::a('点击', ['sign/sign','id' => '01']) ? ...

  2. XML中的转义字符

    HTML中<, >,&等有特别含义,(前两个字符用于链接签,&用于转义),不能直接使用.使用这三个字符时,应使用他们的转义序列,如下所示: & 或 & &a ...

  3. 基于TCP的网络编程

    HTTP协议,FTP协议等很多广泛应用的协议均基于TCP协议.TCP编程主要为C/S模式,客户端和服务器之间的程序设计存在较大差异. TCP编程框图 服务器调用socket().bind().list ...

  4. Double Dispatch讲解与实例-面试题

    引言 说实话,我看过GoF<Design Patterns>,也曾深深的被李建忠<设计模式>系列Webcast吸引.但是还没有见过“Double Dispatch模式”.的确G ...

  5. 04讲 正确使用heterogeneous类型的元件

    heterogeneous类型的元件1.可能出现的错误     再使用数个heterogeneous 元件的时候会因为分部件的不匹配 2.出现错误的原因原因是这四个运放,软件它并不识别那两个是配在一起 ...

  6. Mysql5.5升级到5.7后MySQLdb不能正常使用的问题解决

    ubuntu系统 报错信息1 Type "help", "copyright", "credits" or "license&qu ...

  7. [LeetCode] Concatenated Words 连接的单词

    Given a list of words (without duplicates), please write a program that returns all concatenated wor ...

  8. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  9. QinQ

    7.3.1 QinQ技术诞生的背景 --<华为交换机学习指南> QinQ最初主要是为扩展VLAN ID空间而产生的,但随着城域以太网的发展以及运营商精细化运作的要求,QinQ的双层标签又有 ...

  10. 【WPF】Combobox指定选中值用selectedValue不是很灵的时候,

    wpf combobox 指定选中的值,前题,combobox是通过数据库绑定的ItemsSource:所以再指定的时候用selectValue不是很成功!我的解决方法是 生成一个字典,办值和索引对应 ...