47.Majority Element I & II
Majority Element I
描述
给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一。
You may assume that the array is non-empty and the majority number always exist in the array.
样例
给出数组[1,1,1,1,2,2,2],返回 1
挑战
要求时间复杂度为O(n),空间复杂度为O(1)
class Solution:
"""
@param: nums: a list of integers
@return: find a majority number
"""
def majorityNumber(self, nums):
# write your code here
tmp = nums[0]
count = 1
for num in nums[1:]:
if count == 0 :
tmp = num
count = 1
if tmp == num :
count+=1
else:
count-=1
return tmp
Majority Element II
描述
给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一。
数组中只有唯一的主元素
样例
给出数组[1,2,1,2,1,3,3] 返回 1
挑战
要求时间复杂度为O(n),空间复杂度为O(1)。
class Solution:
"""
@param: nums: a list of integers
@return: The majority number that occurs more than 1/3
"""
def majorityNumber(self, nums):
# write your code here
if not nums:
return []
count1, count2, candidate1, candidate2 = 0, 0, 0, 0
for n in nums:
if n == candidate1:
count1 += 1
elif n == candidate2:
count2 += 1
elif count1 == 0:
candidate1, count1 = n, 1
elif count2 == 0:
candidate2, count2 = n, 1
else:
count1, count2 = count1 - 1, count2 - 1
#return [n for n in (candidate1, candidate2) if nums.count(n) > len(nums) // 3]
if nums.count(candidate1) > len(nums) // 3 :
return candidate1
elif nums.count(candidate2) > len(nums) // 3 :
return candidate2
47.Majority Element I & II的更多相关文章
- LeetCode Majority Element I && II
原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...
- 47 Majority Element II
原题网址; https://www.lintcode.com/problem/majority-element-ii/ 描述 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三 ...
- Majority Element I&II
题号:169, 229. 思路:当要找到超过n/c的元素x时,在更新candidates时,要保证,是x时加1,不是x时消掉c-1.则最终,x一定会出现在一个candidate中.
- LeetCode(169)Majority Element and Majority Element II
一个数组里有一个数重复了n/2多次,找到 思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置. class Solution { public: int majorit ...
- Majority Element,Majority Element II
一:Majority Element Given an array of size n, find the majority element. The majority element is the ...
- LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- Majority Element(169) && Majority Element II(229)
寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法)1.Majority Element 1)description Given an array of si ...
- 【LeetCode】229. Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
随机推荐
- Vue项目使用webstorm开发 保存浏览器不自动更新问题
1.首先进去编辑器的设置页面 2.按照以下步骤进行操作,把如下选项的√去掉即可:
- leetcode第一刷_Merge Intervals
看到这个题我就伤心啊,去微软面试的时候,第一个面试官让我做的题目就是实现集合的交操作,这个集合中的元素就像这里的interval一样.是一段一段的.当时写的那叫一个慘不忍睹.最后果然被拒掉了. .好好 ...
- RocketMQ实现事务消息
在RocketMQ4.3.0版本后,开放了事务消息这一特性,对于分布式事务而言,最常说的还是二阶段提交协议,那么RocketMQ的事务消息又是怎么一回事呢,这里主要带着以下几个问题来探究一下Rocke ...
- C# Lambda 表达式学习之(三):动态构建类似于 c => c.Age == null || c.Age > 18 的表达式
可能你还感兴趣: 1. C# Lambda 表达式学习之(一):得到一个类的字段(Field)或属性(Property)名,强类型得到 2. C# Lambda 表达式学习之(二):LambdaExp ...
- Linux:Day24(下) samba
samba: smb:Service Message Block 是一种协议 CIFS:Common Internet File System smb --> samba 137/udp,138 ...
- (4)HomeAssistant 语言控制
中文教程:https://www.hachina.io/docs/2073.html 英文网教程:https://www.home-assistant.io/components/conversati ...
- Consul在.Net Core中初体验
Consul在.Net Core中初体验 简介 在阅读本文前我想您应该对微服务架构有一个基本的或者模糊的了解 Consul是一个服务管理软件,它其实有很多组件,包括服务发现配置共享键值对存储等 本文主 ...
- springmvc中messageConverter用法
解决StringHttpMessageConverter乱码问题问题: 当我们将字符串对象通过springmvc传回浏览器时,因为StringHttpMessageConverter消息转换器中默认的 ...
- i春秋-百度杯十月场-fuzzing
1. 打开链接,提示 show me key,抓包,传值key=1,GET请求没有用,而POST请求有返回. 2.将md5值直接拿去解密,得到key=ichunqiu105 OK,进入下一步. ...
- python读写修改配置文件(ini)
python 有时候参数需要保存到配置文件中 接下来总结一下 配置文件的读写和修改的操作 代码如下: #!/usr/bin/env python # -*- coding: utf- -*- # 读 ...