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. Js多国时间动态更新

    Js多国时间动态更新 点击下载

  2. ATI显卡添加自定义分辨率

    run regedit, 浏览到这个键目录下 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Video 先导出一个作为备份. 查找 DALNo ...

  3. mvc webapi 返回字符串自动加双引号

    来自:http://www.cnblogs.com/David-Huang/p/4351023.html 返回字符串,突然碰到双引号号问题,幸亏有人解决了. 返回XMLDocument类型,默认会解析 ...

  4. opencv 中对一个像素的rgb值或像素值进行操作的几个常用小办法【转】

    You can access the Image pixels in many ways:1. One using the Inbuilt macro2. One using the pointer ...

  5. 继续node爬虫 — 百行代码自制自动AC机器人日解千题攻占HDOJ

    前言 不说话,先猛戳 Ranklist 看我排名. 这是用 node 自动刷题大概半天的 "战绩",本文就来为大家简单讲解下如何用 node 做一个 "自动AC机&quo ...

  6. 理解JavaScript中的参数传递 - leetcode189. Rotate Array

    1.关于leetcode 这是第一篇关于leetcode的题解,就先扯点关于leetcode的话. 其实很早前就在博客园看到过leetcode一些题解,总以为跟一般OJ大同小异,直到最近点开了一篇博文 ...

  7. Web软件安全攻击

  8. operating expense & captial expenditure

    营运成本(营业成本, operating expense, OPEX) 指的是运行企业的持续性.消耗性的支出,与之对照的是资本支出(captial expenditure, CAPEX).例如:购买影 ...

  9. Python学习教程

    Python语法简洁清晰,特色之一是强制用空白符(white space)作为语句缩进.Python具有丰富和强大的库.它常被昵称为胶水语言,能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地 ...

  10. stack 栈的实现

    今天晚上去「南哪」听了场AI的讲座,除了话筒真心不给力之外,算是对微软这方面的进展有了更多了解,毕竟是半宣传性质的活动吧. 光听这些是没用的,眼下还是打好基础,多尝试学点新技术,拓宽能力和视野比较重要 ...