题目链接: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. Tomcat 服务应用

    转自:http://wiki.jikexueyuan.com/project/tomcat/windows-service.html Tomcat8 是一个服务应用,能使 Tomcat 8 以 Win ...

  2. 给Eclipse提速的7个技巧

    这篇文章只是关注如何让Eclipse运行得更快.每个技巧都针对Windows.Linux和MacOS用户详细说明.在使用所有优化技巧之后,Eclipse应该能在10秒内启动,并且比以前运行得更流畅. ...

  3. 使用JavaMail发送邮件

    一.邮件的相关概念 邮件协议.主要包括: SMTP协议:Simple Mail Transfer Protocol,即简单邮件传输协议,用于发送电子邮件 POP3协议:Post Office Prot ...

  4. ActiveMQ笔记(1):编译、安装、示例代码

    一.编译 虽然ActiveMQ提供了发布版本,但是建议同学们自己下载源代码编译,以后万一有坑,还可以尝试自己改改源码. 1.1 https://github.com/apache/activemq/r ...

  5. LZ77压缩算法编码原理详解(结合图片和简单代码)

    前言 LZ77算法是无损压缩算法,由以色列人Abraham Lempel发表于1977年.LZ77是典型的基于字典的压缩算法,现在很多压缩技术都是基于LZ77.鉴于其在数据压缩领域的地位,本文将结合图 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  8. [LeetCode] Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  9. 使用Bandwagon的VPS第一件事《FQ》

    说点闲话:昨天的长靴子到了,哎呀,今天那个高兴,踩着我的8厘米的过膝靴就出门上专业外语去了,扎了个麻花辫子,那个心情好哟,搞得我都不想继续学习linux平台上的C语言了,好想逛街----秀秀我的鞋子, ...

  10. jquery网页可见区域宽

    网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...