Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

这一题可以用排序之后查看序列正中间那个元素的方法来解。但是复杂度是排序的复杂度nlog(n)。 用如下的方法借鉴Moore Voting。

class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
""" major = 0
count = 0 for x in nums:
if count == 0:
major = x
count = 1
elif x == major:
count += 1
else:
count -= 1 return major

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.

这一题由于对复杂度的限制是线性的,所以不能用排序。可以使用两个majority的candadite。

class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
if not nums:
return []
n1, n2, c1, c2 = 0, 1, 0, 0 for num in nums:
if num == n1:
c1 += 1
elif num == n2:
c2 += 1
elif c1 == 0:
n1 = num; c1 = 1
elif c2 == 0:
n2 = num; c2 = 1
else:
c1 -= 1; c2 -= 1
size = len(nums) return [n for n in (n1, n2) if nums.count(n) > size/3]

Leetcode # 169, 229 Majority Element I and II的更多相关文章

  1. 【LeetCode】229. Majority Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...

  2. 【LeetCode】229. Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  3. 【刷题-LeetCode】229. Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  4. LeetCode OJ 229. Majority Element II

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

  5. 【LeetCode 169】Majority Element

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

  6. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  7. [LeetCode] 229. Majority Element II 多数元素 II

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

  8. 229. Majority Element II My Submissions Question

    Total Accepted: 23103 Total Submissions: 91679 Difficulty: Medium Given an integer array of size n, ...

  9. 229. Majority Element II -- 找出数组中出现次数超过 ⌊ n/3 ⌋ 次的数

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

随机推荐

  1. Hash链表

    <?php /* +------------------------------------------------------------------------------ | dateti ...

  2. 50个查询系列-第13个查询:把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;

    UPDATE tblscore SET tblscore.Score= ( -- 这里开始算叶平的平均值 SELECT AVG(tt.aa) FROM ( SELECT tblscore.Score ...

  3. 使用PHP获取根域名的方法!

    /** * 取得根域名 * @param type $domain 域名 * @return string 返回根域名 */ function GetUrlToDomain($domain) { $r ...

  4. blogs for learning java

    曹海成的专栏 http://blog.csdn.net/caohaicheng/article/details/38071097 http://blog.csdn.net/a5489888/artic ...

  5. CSS3 3D骰子

    z zz zz zzzz zzzzz zzzzzz

  6. 学习C++.Primer.Plus 5 循环和关系表达式

    C++将赋值表达式的值定义为左侧成员的值 赋值操作符是自右向左结合的 cout.setf(ios:: boolalpha);//调用设置标记,命令cout输出true或false,而非1或0. 任何表 ...

  7. Java程序设计的DOS命令基础

    Java程序设计的DOS命令基础 用户使用操作系统和软件有两种方式:命令行界面(Command Line Interface,CLI)和图形界面(Graphical User Interface,GU ...

  8. SM2国密证书合法性验证

    通常我们遇到过的X509证书都是基于RSA-SHA1算法的,目前国家在大力推行国密算法,未来银行发行的IC卡也都是基于PBOC3.0支持国密算法的,因此我们来学习一下如何验证SM2国密证书的合法性.至 ...

  9. Pattern Recognition And Machine Learning读书会前言

    读书会成立属于偶然,一次群里无聊到极点,有人说Pattern Recognition And Machine Learning这本书不错,加之有好友之前推荐过,便发了封群邮件组织这个读书会,采用轮流讲 ...

  10. idea 生成代码中带参数final修饰