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.

Hint:

  1. How many majority elements could it possibly have?
  2. Do you have a better hint? Suggest it!
Subscribe to see which companies asked this question
还是用抵消的办法
开始的参数是0.5,通过了
后来改成1 ,也通过了……
class Solution(object):def majorityElement(self, nums):a = '' #nums[0]b = '' #nums[1]ta = 0tb = 0l = len(nums)if l == 2:return list(set(nums))times = l/3for i in nums[:]:if i == a:ta += 1elif i == b:tb += 1elif ta <= 0:a = ita = 1elif tb <= 0:b = itb = 1else:ta -= 1tb -= 1res = []if nums.count(a) > times:res += a,if a != b and nums.count(b) > times:res += b,return res

229. Majority Element II 【M】【52】相关推荐

  1. LeetCode 229 : Majority Element II

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

  2. leetcode 229. Majority Element II(多数投票算法)

    就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public:vector<int&g ...

  3. 229. Majority Element II**

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

  4. [leetcode] 229. Majority Element II

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

  5. 229. Majority Element II

    class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :r ...

  6. leetcode 229. Majority Element II | 229. 求众数 II(找出现次数超过n/k的元素)

    题目 https://leetcode.com/problems/majority-element-ii/ 题解 思路来源于左程云<程序员代码面试指南> 问题描述 原问题:给定一个整型数组 ...

  7. Majority Element(169) Majority Element II(229)

    寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法) 1.Majority Element 1)description Given an array of s ...

  8. [LeetCode] Majority Element II

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

  9. 【Leet Code】229. Majority Element II---Medium

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

最新文章

  1. Linux下的USB总线驱动 mouse
  2. 关于ubuntu的root密码问题
  3. SpringBatch 配置并行启动Job详解 (八)
  4. sed搜索某行在行末追加_linux shell 用sed命令在文本的行尾或行首添加字符
  5. java表达式语句_Java基础知识笔记第三章:运算符表达式语句
  6. nginx 代理静态资源报 403
  7. DAO基本登录(1)
  8. Unity WebGL自带VideoPlayer播放视频
  9. cαr怎么发音_英语c的发音怎么读
  10. SPSS学习(四)单样本t检验
  11. 今日头条推荐算法原理全文详解之六
  12. dreamware html中加入flv,Dreamweaver插入FLV文件技巧
  13. bable.js是什么
  14. mp3转成m4a的简单方法介绍
  15. [UNR#5]诡异操作
  16. PMP项目管理中的重要角色
  17. Dropbox 架设免费个人网站
  18. ZETA 一种基于 UNB 的低功耗广域网 (LPWAN)技术
  19. 局域网用程控交换机电源的网络监控系统设计
  20. HTTP Status 400 (Failed to load resource: the server responded with a status of 400)

热门文章

  1. Ubuntu: failed to fetch
  2. 大学生html5设计大赛方案,某企业举办创意设计大赛,大学生小云经过反复酝酿,确定了创意主题和设计方案,并邀请小...
  3. siTOOLs Biotech中国总代理蓝景科信 riboPOOL植物通用核糖体RNA(rRNA)去除探针试剂盒——高效去除开花植物(被子植物)核糖体RNA
  4. Python编写程序,生成包含20个随机数列表,然后将前十个元素升序排列,后十个元素降序排列,并输出结果。
  5. 现今活跃在信息技术领域的卓越作者--张亚飞新作上市
  6. LeetCode 110 Balanced Binary Tree
  7. 计算机英语性考任务答案,国家开放大学(中央电大)本科《理工英语4》网上形考任务试题及答案...
  8. 鸿蒙os商用版,鸿蒙OS再次官宣最快今年9月商用,可以一夜之间代替安卓系统
  9. work转换html,翻转换图.html · 张斯佗罗夫柯基/personal work notes - Gitee.com
  10. 2013年5月中国数码相机市场分析报告