leetcode--Majority Element
题目链接: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的更多相关文章
- 2016.5.18——leetcode:Majority Element
Majority Element 本题收获: 1.初步了解hash,nth_element的用法 2.题目的常规思路 题目: Given an array of size n, find the ma ...
- [LeetCode] Majority Element II 求众数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- [LeetCode] Majority Element 求众数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode Majority Element I && II
原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...
- [LeetCode] Majority Element II 求大多数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...
- [LeetCode] Majority Element 求大多数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode Majority Element I
原题链接在这里:https://leetcode.com/problems/majority-element/ 题目: Given an array of size n, find the major ...
- LeetCode——Majority Element
在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素.容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element. ...
- LeetCode Majority Element Python
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Leetcode: Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
随机推荐
- linux 做gw(nat)详细配置
linux 做企业网关gw(nat)详细配置 最近因为公司的路由器老化导致上网时断时续,上半小时网就断一次网,为此我头疼不已,本着为公司节约成本的宗旨, ...
- android socket 线程连接openwrt与arduino单片机串口双向通信
package zcd.netanything; import java.io.BufferedReader; import java.io.InputStreamReader; import jav ...
- JVM原理和优化
JVM工作原理和特点主要是指操作系统装入JVM是通过jdk中Java.exe来完成,通过下面4步来完成JVM环境. 1.创建JVM装载环境和配置 2.装载JVM.dll 3.初始化JVM.dll并挂界 ...
- angularJS(3)
angularJS(3) 一.angularJs的指令模型ng-model指令 ng-model 指令 绑定 HTML 元素 到应用程序数据. 为应用程序数据提供类型验证(number.email ...
- PAT 1049. 数列的片段和(20)
给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段.例如,给定数列{0.1, 0.2, 0.3, 0.4},我们有(0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1 ...
- 部署 DevStack - 每天5分钟玩转 OpenStack(17)
本节按照以下步骤部署 DevStack 实验环境,包括控制节点和计算节点 创建虚拟机 按照物理资源需求创建 devstack-controller 和 devstak-compute 虚拟机 安装操作 ...
- [LeetCode] Wiggle Sort 摆动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- SQLite源程序分析之sqlite3.c
/****************************************************************************** ** This file is an a ...
- 关于EventSource的精华
他是keep-alive的连接,服务端持续向这个请求的Reponse发送数据,以"data: "+您的消息+"\n\n"的格式发送,浏览器端会收到您发送的消息. ...